JS reserved decimal point (rounded, four homes six into) the realization of ideas and examples _javascript skills

Source: Internet
Author: User
Tags pow reserved
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>floatDecimal.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<script type= "Text/javascript" >
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;
}
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
Document.writeln ("Retain 2 decimal places:" + todecimal (3.14159267) + ' <br/> ');
Document.writeln ("Mandatory retention of 2 decimal places:" + toDecimal2 (3.14159267) + ' <br/> ');
Document.writeln ("Retain 2 decimal places:" + todecimal (3.14559267) + ' <br/> ');
Document.writeln ("Mandatory retention of 2 decimal places:" + toDecimal2 (3.15159267) + ' <br/> ');
Document.writeln ("Retain 2 decimal places:" + fomatfloat (3.14559267, 2) + ' <br/> ');
Document.writeln ("Retain 1 decimal places:" + fomatfloat (3.15159267, 1) + ' <br/> ');
Five homes six into
Document.writeln ("Retain 2 decimal places:" + 1000.003.toFixed (2) + ' <br/> ');
Document.writeln ("Retain 1 decimal places:" + 1000.08.toFixed (1) + ' <br/> ');
Document.writeln ("Retain 1 decimal places:" + 1000.04.toFixed (1) + ' <br/> ');
Document.writeln ("Retain 1 decimal places:" + 1000.05.toFixed (1) + ' <br/> ');
Scientific count
Document.writeln (3.1415.toExponential (2) + ' <br/> ');
Document.writeln (3.1455.toExponential (2) + ' <br/> ');
Document.writeln (3.1445.toExponential (2) + ' <br/> ');
Document.writeln (3.1465.toExponential (2) + ' <br/> ');
Document.writeln (3.1665.toExponential (1) + ' <br/> ');
Accurate to n-bit, excluding n-bit
Document.writeln ("Accurate to decimal point 2nd" + 3.1415.toPrecision (2) + ' <br/> ');
Document.writeln ("Accurate to decimal point 3rd" + 3.1465.toPrecision (3) + ' <br/> ');
Document.writeln ("Accurate to decimal point 2nd" + 3.1415.toPrecision (2) + ' <br/> ');
Document.writeln ("Accurate to decimal point 2nd" + 3.1455.toPrecision (2) + ' <br/> ');
Document.writeln ("Accurate to decimal point 5th" + 3.141592679287.toPrecision (5) + ' <br/> ');
</script>
<body>
This is my HTML page. <br>
</body>


JavaScript rounding retains two decimal places

Copy Code code as follows:

function count () {
Alert ("Count");
var size=~~ (document.getElementById ("size"). Value);
var value=0;
for (Var i=0;i<size;i++) {
var val=1* (document.getElementById ("Afteradjscor" +i). Value);
if (null!=val) {
Value =math.round ((1* (value+val)) *100)/100;
}
}
if (isNaN (value)) {
Value= "Input must be a numeric type";
}
document.getElementById ("Total"). Value=value;
}

Description: ~~xx: String int
1*xx: String float
Math.Round ((1* (value+val)) *100)/100: Rounding

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.