How to round a number to its nearest 10's place? Example if number = 123, then it should be rounded to 100, if it is 175 that is greater than 150 then it should round to 200 , the number will changed ?
How to round a number to its nearest 10's place? Example if number = 123, then it should be rounded to 100, if it is 175 that is greater than 150 then it should round to 200 , the number will changed ?
Share Improve this question edited Apr 10, 2015 at 16:38 user2314737 29.4k20 gold badges107 silver badges121 bronze badges asked Feb 27, 2015 at 9:00 JuhanJuhan 1,2912 gold badges12 silver badges32 bronze badges 1- possible duplicate of How to round an integer up or down to the nearest 10 using Javascript – Sadikhasan Commented Feb 27, 2015 at 9:06
7 Answers
Reset to default 6You can use Math.round()
for that. Math.round()
rounds of the decimals. In order to get decimals in this case we need to divide a
with 100 (155/100 = 1.55). Now the Math.round()
will round this number to 2. We can now multiply this number with 100 again to get the correct number.
Try this:
var number = 155;
var rounded = Math.round( number / 100 ) * 100;
this is how I would do it,
function round10(num){
var tmp=1;
while(num>10){
num = Math.round(num/10);
tmp*=10;
}
return num*tmp;
}
<script>
var num=230;
num=((num/100).toFixed(0))*100;
</script>
try this var num = 175; alert(Math.round(num/100)*100);
Divide the number by 100 and round it. Then, multiply by 100.
num = Math.round(num / 100) * 100;
In dart you can:
var number = 143;
var result = (number / 100).round() * 100
value = 2345;
log_10 = parseInt(Math.log(value) / Math.log(10));
basePower = Math.pow(10, log_10);
result = ((((value/basePower)).toFixed(0))* basePower);
alert(result);