Code
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/
--><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>javascript Rounding (Math.Round () and Math.pow ()) </title>
<script type= "Text/javascript" >
Math.Round (x); Returns the nearest integer of the number, rounded to the integer, which is the fractional part of the number
function f () {
Alert (Math.Round (123.567));
Alert (Math.Round (123.456));
}
Math.pow (x, y); Returns the specified power to the base
Returns a numeric expression that is the Y power of X, equal to the Y power of X
If the POW parameter is too large to cause a floating-point overflow, return infinity
Function F1 () {
Alert (Math.pow (2,10));//2 10 times equals 1024.
Alert (Math.pow (1024,0.1));//1024 0.1 times equals 2.
Alert (Math.pow (99,9999));//Overflow returns infinity
}
/*javascript set the number of decimal digits to keep, rounded.
*fordight (dight,how): Numeric formatting function, dight the number to be formatted, how to preserve the number of decimal digits.
* The method here is to multiply by 10, then subtract the decimal and then divide it by a multiple of 10.
*/
function Fordight (dight,how) {
Dight = Math.Round (Dight*math.pow (10,how))/math.pow (10,how);
return dight;
}
function F2 () {
Alert (Fordight (12345.67890,3));//reserved Three decimal places
Alert (Fordight (123.99999,4));//reserved Four decimal places
}
Another method of rounding is the same principle.
The two parameters inside: num is the data to be converted. n is the number of bits to convert
Cheng (123.456,2);//reserved Two decimal places
Function Cheng (num,n) {
var dd=1;
var tempnum;
for (i=0;i<n;i++) {
dd*=10;
}
Tempnum = NUM*DD;
Tempnum = Math.Round (tempnum);
alert (TEMPNUM/DD);
}
</script>
<body>
<input type= "button" value= "Round" onclick= "f ();"/>
<input type= "button" value= "POW" onclick= "F1 ();"/>
<input type= "button" value= "sets the number of decimal digits to keep, rounding" onclick= "F2 ();"/>
<input type= "button" value= "Cheng" onclick= "Cheng (123.456,2);"/>
</body>
Code
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/
--><script type= "Text/javascript";
//Use JavaScript to take the float type decimal point two, example 22.127456 to 22.13, how to do?
//1. The stupidest way .... [I'll do what I do ...]
function Get () {
var s = 22.127456 + "";
var str = s.substring (0,s.indexof (".") + 3);
alert (str);
}
</script>
<script type= "Text/javascript";
//2. Regular expression works well
onload = function () {
var a = "23.456322";
var anew;
var re =/([0-9]+\.[ 0-9]{2}) [0-9]*/;
anew = A.replace (Re, "$");
alert (anew);
}
</script>
<script type= "Text/javascript" >
3. He is more clever ...
var num=22.127456;
Alert (Math.Round (num*100)/100);
</script>
<script type= "Text/javascript" >
4. Friends who will use new things .... But we need ie5.5+ to support it.
var num=22.127456;
Alert (num.tofixed (2));
</script>
Article from: http://my.oschina.net/haquanwen/blog/144033?p=1
JavaScript rounding (Math.Round () and Math.pow ())