admin管理员组文章数量:1287511
How I can round a value maintaining 2 decimal places? I have tried using Math.round(val)
but doesn't work.
Takes these numbers as examples:
25
should be converted to25.00
25.3666
should be rounded to25.37
25.55333
should be rounded to25.55
How I can round a value maintaining 2 decimal places? I have tried using Math.round(val)
but doesn't work.
Takes these numbers as examples:
25
should be converted to25.00
25.3666
should be rounded to25.37
25.55333
should be rounded to25.55
- Use .toFixed(2); --- like parseFloat("123.456789").toFixed(2); – prog1011 Commented Apr 25, 2017 at 5:59
- var m = parseFloat(fullamount).toFixed(2); Its working fine. Thanks – Jagadeesh Commented Apr 25, 2017 at 6:13
4 Answers
Reset to default 5Have a look at toFixed()
.
var a = 2;
a = a.toFixed(2);
now a is 2.00
Please note, that the returned value is a string, use with caution.
Math.round(num * 100) / 100
This should work.
There are two ways of doing it:
first:
use Math.ceil for round up
for eg:
Math.ceil("your digits");
Also you can use the following
parseFloat("123.456").toFixed(2);
Use .toFixed() function as below.
var num = 5.56789;
var n = num.toFixed(2);
// OUTPUT n = 6.57
The toFixed() method converts a number into a string, keeping a specified number of decimals.
本文标签: javascriptRound values with 2 decimal places and add 0 if not decimal are presentStack Overflow
版权声明:本文标题:javascript - Round values with 2 decimal places and add 0 if not decimal are present - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741311544a2371676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论