jquery - Round to minimum of 2 decimals and max of 4 decimals in Javascript - Stack Overflow

Is there a way to round number to min 2 decimals i javascript.Example:10 -> 10.0020.10 -> 20.10

Is there a way to round number to min 2 decimals i javascript.

Example:

  1. 10 -> 10.00
  2. 20.10 -> 20.10
  3. 30.1234 -> 30.1234
  4. 40.123456 -> 40.1235
  5. 50.1200 -> 50.12
  6. 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:

  1. 10 -> 10.00
  2. 20.10 -> 20.10
  3. 30.1234 -> 30.1234
  4. 40.123456 -> 40.1235
  5. 50.1200 -> 50.12
  6. 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 as 40.1235? (10 to 10.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" but 10 is "10.00" (and not "10.000" or "10.0000")? And would 123.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
 |  Show 1 more ment

4 Answers 4

Reset to default 6

If 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信