See a lot of people have this reserved number of decimal point after the demand, but most of them write a function to intercept, but also consider rounding up what, it is quite complicated to write.
In fact, the number object of JavaScript has a method of preserving decimals after the decimal point: toFixed, which is rounded.
I was afraid IE6 not support this method, see MDN Inside said this method is javascript1.5 only came out. Specifically under the IE6 to try, is fully supported
Toexponential ([fractiondigits]): Returns the number in scientific notation format, where the fractiondigits value is retained after the decimal point.
ToFixed ([fractiondigits]): Returns a number to the specified number of decimal places, where the fractiondigits value is retained after the decimal point.
Toprecision ([precision]): Returns a number to the specified precision (this precision does not refer to a few after the decimal point), where precision is the specified precision value.
Examples are as follows:
The code is as follows |
Copy Code |
var n = 12345.6789; N.tofixed (); Returns 12346 N.tofixed (1); Returns 12345.7 N.tofixed (6); Returns 12345.678900 (1.23e+20). toFixed (2); Returns 123000000000000000000.00 (1.23e-10). toFixed (2); Returns 0.00 2.34.toFixed (1); Returns 2.3 -2.34.tofixed (1); Returns-2.3 ( -2.24). toFixed (1); Returns-2.2 |
Conversion function, this code comes from a foreign forum.
The code is as follows |
Copy Code |
function Roundnumber (number,decimals) { var newstring;//the new rounded number Decimals = number (decimals); if (Decimals < 1) { NewString = (Math.Round (number)). ToString (); } else { var numstring = number.tostring (); if (Numstring.lastindexof (".") = = 1) {//If there no decimal point Numstring + = ".";/ /Give it one at the end } var cutoff = Numstring.lastindexof (".") + decimals;//the point in which to truncate the number var D1 = number (numstring.substring (cutoff,cutoff+1));/The value of the last decimal place, we ' ll end var d2 = number (numstring.substring (cutoff+1,cutoff+2));/The next decimal, after the last one we want if (D2 >= 5) {//Do we need to round up on all? If not, the string would just be truncated if (D1 = = 9 && cutoff > 0) {//If the digit is 9, find a new cutoff point while (Cutoff > 0 && (d1 = 9 | | | isNaN (d1))) { if (D1!= ".") { cutoff-= 1; D1 = number (numstring.substring (cutoff,cutoff+1)); } else { cutoff-= 1; } } } D1 + 1; } if (D1 = = 10) { numstring = numstring.substring (0, Numstring.lastindexof (".")); var roundednum = number (numstring) + 1; NewString = roundednum.tostring () + '. '; } else { NewString = numstring.substring (0,cutoff) + d1.tostring (); } } if (Newstring.lastindexof (".") = = 1) {//Do this again, to the new string NewString + = "."; } var decs = (newstring.substring (Newstring.lastindexof (".") +1)). length; for (Var i=0;i<decimals-decs;i++) newstring + = "0"; var newnumber = number (newstring)//Make it a number if Document.roundform.roundedfield.value = newstring; Output to the form field (change for your purposes) } |