Implement JS to retain the decimal point after the N-bit code

Source: Internet
Author: User
Tags pow

In JS, the general implementation of preserving the decimal point after N, are using the tofixed function

The

code is as follows:


<script language= "JavaScript" >


document.write ("<h1>js keep two decimal examples </h1><br>");


var a=2.1512131231231321;


document.write ("Original value:" +a+ "<br>");


document.write ("Two decimal point:" +a.tofixed (2) + "<br> Four decimal Places" +a.tofixed (4));


</script>

Rounding of the conversion function, as follows:

The

code is as follows:


function Round (v,e) {


var t=1;


for (; e>0;t*=10,e--);


for (; e<0;t/=10,e++);


return Math.Round (v*t)/t;


}

In the parameters:

V represents the value to convert

E indicates the number of digits to keep

The two for in the function, this is the point,

The first for the right side of the decimal point, that is, how many digits to the right of the decimal point;

The second for is for the left of the decimal point, which is how many digits to the left of the decimal point.

For is to calculate the value of T, which is how many times v should be enlarged or shrunk (multiples =t).

For here the two features of the for, conditional judgment and counter cumulation (loops) are used.

When e satisfies a condition for continuation, and E accumulates every time (the sum of E is given to a for-manufacture condition that does not satisfy the cycle), also calculates the value of T.

Finally, the native round method is used to calculate the result of the enlarged/narrowed V, and then the result is enlarged/reduced to the correct multiple.

The following two-digit examples of retention

The

code is as follows:


<script type= "Text/javascript" >


//Reserved Two decimal


//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;


}


//system to retain 2 decimal places, such as: 2, will be 2 after the completion of 00. That is 2.00


function ToDecimal2 (x) {


var f = parsefloat (x);


if (isNaN (f)) {


return false;


}


var f = math.round (x*100)/100;


var s = f.tostring ();


var rs = s.indexof ('. ');


if (Rs < 0) {


rs = s.length;


s + = '. ';


}


while (s.length <= RS + 2) {


s + + ' 0 ';


}


return s;


}


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 ("Retain 2 decimal places:" + 1000.003.toFixed (2));


alert ("Retain 1 decimal places:" + 1000.08.toFixed (1));


alert ("Retain 1 decimal places:" + 1000.04.toFixed (1));


alert ("Retain 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));


</script>

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.