admin管理员组文章数量:1241082
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);
本文标签: Rounding a number to its nearest 1039s place javascriptStack Overflow
版权声明:本文标题:Rounding a number to its nearest 10's place javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740058890a2222506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论