0

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

Community
  • 1
  • 1
cyberfly
  • 5,568
  • 8
  • 50
  • 67

5 Answers5

2

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);
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
1

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().

DanielB
  • 19,910
  • 2
  • 44
  • 50
1

You can try something like this :

var mynum = 192.168;
var result=Math.round(mynum*100)/100; 
Awea
  • 3,163
  • 8
  • 40
  • 59
0

Very simple

testVal = Math.round(YourNumberHere*100)/100 ;
Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49
0

Aren't you simply trying to floor? That is, "round down".

Math.floor(the_number*10)/ 10)
RichN
  • 6,181
  • 3
  • 30
  • 38