Is there a way to round number to min 2 decimals i javascript.
Example:
- 10 -> 10.00
- 20.10 -> 20.10
- 30.1234 -> 30.1234
- 40.123456 -> 40.1235
- 50.1200 -> 50.12
- 60.123 -> 60.123
And so on... so round to min 2 decimals. I can use jquery for this.
Is there a way to round number to min 2 decimals i javascript.
Example:
- 10 -> 10.00
- 20.10 -> 20.10
- 30.1234 -> 30.1234
- 40.123456 -> 40.1235
- 50.1200 -> 50.12
- 60.123 -> 60.123
And so on... so round to min 2 decimals. I can use jquery for this.
Share Improve this question edited Nov 28, 2016 at 17:49 Sensei asked Nov 28, 2016 at 17:34 SenseiSensei 7510 bronze badges 6-
4
What's the rule to round
40.123456
as40.1235
? (10
to10.00
is not even rounding.) – Álvaro González Commented Nov 28, 2016 at 17:36 - 1 Sorry, The point is to round to min 2 decimals (if there are no decimals it would be like formating the number) and to max of 4 decimals. – Sensei Commented Nov 28, 2016 at 17:39
-
So what determines that
40.123456
bees"40.1235"
but10
is"10.00"
(and not"10.000"
or"10.0000"
)? And would123.456
be"123.456"
or"123.46"
or"123.4560"
? (I'm using quotes because we are talking about presentation here, right? Not numeric value?) – T.J. Crowder Commented Nov 28, 2016 at 17:40 - 1 Rounding is not the problem here, the number of decimals are my issue sorry if I was not clear. @T.J.Crowder – Sensei Commented Nov 28, 2016 at 17:41
- 123.456 -> 123.456 @T.J.Crowder – Sensei Commented Nov 28, 2016 at 17:47
4 Answers
Reset to default 6If you are not restricted to outdated browsers I remend this smart feature:
var fmtr = new Intl.NumberFormat('us-us', {
style: 'decimal',
useGrouping: false,
minimumFractionDigits: 2,
maximumFractionDigits: 4
});
function test(test) {
var result = fmtr.format(test.num);
console.log(
test.num,
"=>",
result,
result == test.result ? "- Pass" : "- Fail"
);
}
[
{num: 10, result: "10.00"},
{num: 20.10, result: "20.10"},
{num: 30.1234, result: "30.1234"},
{num: 40.123456, result: "40.1235"},
{num: 50.1200, result: "50.12"},
{num: 60.123, result: "60.123"}
].forEach(test);
References:
- MDN
- Support Status on caniuse
- Specification: Current | Next Draft
It sounds like you're saying you want to turn numbers into strings with at least two decimal places (even if they're zeros) and up to four if necessary (they aren't zeros). If so:
For all modern browsers: See Marek's answer.
If you can't rely on Intl
: The only thing I can think of is to use toFixed(4)
and then remove up to two trailing zeros:
function format(num) {
var str = num.toFixed(4);
return str.replace(/0{1,2}$/, '');
}
function test(test) {
var result = format(test.num);
console.log(
test.num,
"=>",
result,
result == test.result ? "- Pass" : "- Fail"
);
}
[
{num: 10, result: "10.00"},
{num: 20.10, result: "20.10"},
{num: 30.1234, result: "30.1234"},
{num: 40.123456, result: "40.1235"},
{num: 50.1200, result: "50.12"},
{num: 60.123, result: "60.123"}
].forEach(test);
Have a look at the toFixed() javascript function: here
It leaves it as a string though which may cause a problem depending on what you're doing
The chosen solution only works for 4 decimals. Not 5 or more. If you are restricted to outdated browsers, the robust code below removes all trailing zeros and then adds 2 zeros if none left.
function format(n) {
let r = (+n).toFixed(10); // or any
if (r.match(/\./)) {
r = r.replace(/\.?0+$/, '');
}
const s = r.toString();
if (s.indexOf('.') === -1) {
r = Number.parseFloat(r).toFixed(2);
}
return r;
}
I needed something like this but that could work with any amount of decimals and not restricted to outdated browsers.
This is one of the possible solutions not yet presented, which I posted with the hope someone could ment how to simplify this to regex only. Thanks!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744163057a4561180.html
评论列表(0条)