Click to See Complete Forum and Search --> : Rounding numbers


David Harrison
12-20-2002, 03:32 PM
How would I be able to round a number to 2 decimal places.
eg. 873.759 woulb round to 873.76

but also in this next case:

eg. 999.999 would round to 1000.00

I need it to display the 2 0's after the point.

If you can understand my random mumblings then please reply.

Charles
12-20-2002, 04:35 PM
<script type="text/javascript">
<!--
Math.centiRound = function (num) {return Math.round(num*100) / 100}
alert (Math.centiRound(999.999));
// -->
</script>

swon
12-21-2002, 09:39 AM
Above a good script from Charles, here another example:


<body>
<form name="form" action="">
<input type=text name="Input">
<input type=text name="Output">
<input type="button" value="Round" onClick="form.Output.value=Math.round(form.Input.value)">
</form>
</body>

Charles
12-21-2002, 01:26 PM
Originally posted by swon
Above a good script from Charles, here another example:


<body>
<form name="form" action="">
<input type=text name="Input">
<input type=text name="Output">
<input type="button" value="Round" onClick="form.Output.value=Math.round(form.Input.value)">
</form>
</body> But that will round to an integer. The inquirer wanted to know how to round to the hundredth.

David Harrison
12-22-2002, 09:02 AM
A good final point Charles but I can simply multiply the number by 100, round it and then divide it by 100. However this does mean that, for instance:
The number - 999.999 would round to 1000, so how would I get it to round to 1000.00

khalidali63
12-22-2002, 10:40 AM
I have not come across floar or double values manipulationn through Math object in JavaScript,
For that I think, you may have to create a custome function that will run through the value to .00 to decimal places and then format it and show it.

If it doesnt make any sense,I can probabley produce a method for you just let me know what exactly u have in mind

Khalid

aepstar
12-22-2002, 01:46 PM
http://www.google.nl/search?q=javascript+rounding+numbers&ie=UTF-8&oe=UTF-8&hl=nl&lr=

Charles
12-22-2002, 02:06 PM
JavaScript doesn't give you much to work with in the number department. There's only one kind, eight bit floating-point. There's also a Number class and Number primitives are converted to Number objects as needed. But alert(3.14159) doesn't seem to call Number.toString() like you would expect. And there's no native way to format the string conversion of either the primitive or the class. This seems to work, though:

<script type="text/javascript">
<!--
Number.prototype.format = function (places) {
n = (Math.round(this * Math.pow(10, places))).toString();
s1 = n.slice(0, -(places));
s2 = n.slice(-(places));
return s1+'.'+s2;
}
alert ((999.999).format(2));
// -->
</script>

Charles
12-22-2002, 03:54 PM
There's a wee error on that script above. It doesn't work for numbers zero or less or if you ask for more than 20 places. The following corrects for the former and will only return a max of 20 places:

<script type="text/javascript">
<!--
Number.prototype.format = function (places) {
if (places > 20) places = 20;
n = (Math.round(this * Math.pow(10, places))).toString();
for (i=1; i<=places; i++) {if (n < Math.pow(10,i)) n = '0' + n};
return n.slice(0, -(places)) + '.' + n.slice(-(places));
}
alert ((Math.PI).format(25));
// -->
</script>

Charles
12-23-2002, 01:04 PM
But that doesn' work for negative numbers; this does.

<script type="text/javascript">
<!--
Number.prototype.format = function (places) {
if (places > 20) places = 20;
n = (Math.round(Math.abs(this) * Math.pow(10, places))).toString();
for (i=1; i<=places; i++) {if (n < Math.pow(10,i)) n = '0' + n};
if (this < 0) n = '-' + n;
return n.slice(0, -(places)) + '.' + n.slice(-(places));
}
alert ((-1).format(4));
// -->
</script>

Charles
12-24-2002, 06:18 AM
This might be a better way:From the ECMA-262 Specification (http://www.ecma.ch/ecma1/STAND/ECMA-262.HTM), page 111
15.7.4.5 Number.prototype.toFixed (fractionDigits)
Return a string containing the number represented in fixed-point notation with fractionDigits digits
after the decimal point. If fractionDigits is undefined, 0 is assumed. Specifically, perform the
following steps:
1. Let f be ToInteger(fractionDigits). (If fractionDigits is undefined, this step produces the value
0).
2. If f < 0 or f > 20, throw a RangeError exception.
3. Let x be this number value.
4. If x is NaN, return the string "NaN".
5. Let s be the empty string.
6. If x = 0, go to step 9.
7. Let s be "-".
8. Let x = –x.
9. If x = 1021, let m = ToString(x) and go to step 20.
10. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as
possible. If there are two such n, pick the larger n.
11. If n = 0, let m be the string "0". Otherwise, let m be the string consisting of the digits of the
decimal representation of n (in order, with no leading zeroes).
12. If f = 0, go to step 20.
13. Let k be the number of characters in m.
14. If k > f, go to step 18.
15. Let z be the string consisting of f+1–k occurrences of the character ‘0’.
16. Let m be the concatenation of strings z and m.
17. Let k = f + 1.
18. Let a be the first k–f characters of m, and let b be the remaining f characters of m.
19. Let m be the concatenation of the three strings a, ".", and b.
20. Return the concatenation of the strings s and m.
The length property of the toFixed method is 1.
If the toFixed method is called with more than one argument, then the behaviour is undefined (see
clause 15).
An implementation is permitted to extend the behaviour of toFixed for values of fractionDigits less
than 0 or greater than 20. In this case toFixed would not necessarily throw RangeError for such
values.
NOTE
The output of toFixed may be more precise than toString for some values because toString only
prints enough significant digits to distinguish the number from adjacent number values. For example,
(1000000000000000128).toString() returns "1000000000000000100", while
(1000000000000000128).toFixed(0) returns "1000000000000000128".You'll find that Number.prototype.toFixed is implemented in Mozila 1.2.1, MSIE 6 and NN 7, but not NN4 or Opera 6.