最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript round with a zero at the end (fixed number of decimals) - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited Feb 21, 2013 at 5:47 Danil Speransky 30.5k6 gold badges69 silver badges78 bronze badges asked Oct 1, 2012 at 1:32 nastynasty 7,0879 gold badges41 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You 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'
发布评论

评论列表(0)

  1. 暂无评论