Reputation: 5888
This is a bit different from my question before
How to round up using javascript
I have this set of number and want to round down to 1 decimal point. Since standard javascript round up function cant meet my requirement, hope someone can help me :)
Below is the ouput needed when round down:
192.168 => 192.10
192.11 => 192.10
192.21 =>192.20
192.26 => 192.20
192.30 = > 192.30
Upvotes: 0
Views: 2521
Reputation: 43275
this is not rounding, 192.168 rounded to two decimal places is 192.20.
However for your requirement : You might just want to multiply it by multiples of 10, take 'floor' , and then divide by same number.
see it here : http://jsfiddle.net/7qeGH/
var numbers = new Array(192.168,192.11 ,192.21 ,192.26 ,192.30 );
var response = "";
for(var i =0 ; i< numbers.length ; i++)
{
var rounded = Math.floor(numbers[i]*10)/10 + '0'; // '0' is added as string
response += numbers[i] + " => " + rounded + "\n";
}
alert(response);
Upvotes: 2
Reputation: 6241
Aren't you simply trying to floor? That is, "round down".
Math.floor(the_number*10)/ 10)
Upvotes: 0
Reputation: 3758
Very simple
testVal = Math.round(YourNumberHere*100)/100 ;
Upvotes: 0
Reputation: 3183
You can try something like this :
var mynum = 192.168;
var result=Math.round(mynum*100)/100;
Upvotes: 1
Reputation: 20230
How about Math.round(number * 10) / 10
? You'll get a result with 1 decimal so you can add a zero at the end (Math.round(number * 10) / 10) + "0"
But actually you round down not up. So it would be Math.floor()
.
Upvotes: 1