JavaScript Preserve decimal function in detail

Source: Internet
Author: User
Tags pow

The first, using Math.Round

The code is as follows Copy Code
var original=28.453
1)//round "original" to two decimals
var result=math.round (original*100)/100; Returns 28.45
2)//Round "original" to 1 decimal
var result=math.round (original*10)/10; Returns 28.5

The second, js1.5 above can be used tofixed (x), you can specify the number of intercepted decimal point x

The code is as follows Copy Code

3)//round "original" to two decimals
var result=original.tofixed (2); Returns 28.45

4)//Round "original" to 1 decimal
var result=original.tofixed (1); Returns 28.5

The above two methods are most common, but can not meet certain special requirements, such as retaining two digits after the decimal point, if less than two, less than two bit 0. Now there is a third way.

I've sorted out some of these reserved decimal functions

The code is as follows Copy Code

Keep two decimal digits
Function: Rounding floating-point numbers, taking 2 digits after the decimal point
function ToDecimal (x) {
var f = parsefloat (x);
if (isNaN (f)) {
Return
}
f = Math.Round (x*100)/100;
return F;
}


The system retains 2 decimal places, such as: 2, will be 00 after 2. That is 2.00

Function ToDecimal2 (x) { 
    var f = parsefloat (x); 
    if isNaN (f ) { 
        return false; 
   } 
& nbsp;   var f = math.round (x*100)/100; 
    var s = f.tostring (); 
 &nbs p;  var rs = s.indexof ('. ');  
    if (Rs < 0) { 
        rs = s.length; 
        S + = '. ';  
   } 
    while (s.length <= RS + 2) { 
  &nbs p;     s + = ' 0 '; 
   } 
    return s; 
}& nbsp.

function Fomatfloat (src,pos) {
Return Math.Round (Src*math.pow, POS))/math.pow (POS);
}


Rounded
Alert ("Retain 2 decimal places:" + todecimal (3.14159267));
Alert ("Force retention of 2 decimal places:" + toDecimal2 (3.14159267));
Alert ("Retain 2 decimal places:" + todecimal (3.14559267));
Alert ("Force retention of 2 decimal places:" + toDecimal2 (3.15159267));
Alert ("Retain 2 decimal places:" + fomatfloat (3.14559267, 2));
Alert ("Retain 1 decimal places:" + fomatfloat (3.15159267, 1));

Five homes six into
Alert ("Keep 2 decimal places:" + 1000.003.toFixed (2));
Alert ("Keep 1 decimal places:" + 1000.08.toFixed (1));
Alert ("Keep 1 decimal places:" + 1000.04.toFixed (1));
Alert ("Keep 1 decimal places:" + 1000.05.toFixed (1));

Scientific count
Alert (3.1415.toExponential (2));
Alert (3.1455.toExponential (2));
Alert (3.1445.toExponential (2));
Alert (3.1465.toExponential (2));
Alert (3.1665.toExponential (1));
Accurate to n-bit, excluding n-bit
Alert ("Accurate to decimal point 2nd" + 3.1415.toPrecision (2));
Alert ("Accurate to decimal point 3rd" + 3.1465.toPrecision (3));
Alert ("Accurate to decimal point 2nd" + 3.1415.toPrecision (2));
Alert ("Accurate to decimal point 2nd" + 3.1455.toPrecision (2));
Alert ("Accurate to decimal point 5th" + 3.141592679287.toPrecision (5)); 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.