admin管理员组文章数量:1313819
Im using javascript round
to round the numbers. But when the value is $106.70
the output shows only $106.7
and its missing the last 0
. How can I add a zero for numbers ending with a zero or is there any easier way than using if else to check this?
Im using javascript round
to round the numbers. But when the value is $106.70
the output shows only $106.7
and its missing the last 0
. How can I add a zero for numbers ending with a zero or is there any easier way than using if else to check this?
1 Answer
Reset to default 9You can use the .toFixed
method:
var n = 106.72; console.log(n.toFixed(2)); //=> '106.72'
var n = 106.70; console.log(n.toFixed(2)); //=> '106.70'
var n = 106.7; console.log(n.toFixed(2)); //=> '106.70'
var n = 106; console.log(n.toFixed(2)); //=> '106.00'
And it rounds:
var n = 106.76; console.log(n.toFixed(1)); //=> '106.8'
本文标签: Javascript round with a zero at the end (fixed number of decimals)Stack Overflow
版权声明:本文标题:Javascript round with a zero at the end (fixed number of decimals) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741955162a2406947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论