1

I am developing a REST API and returning JSON. One of the fields is called submissionPercent and I need it to be a number but with exactly 2 decimal places

  • If the submissionPercent is 20, I need to return 20.00.
  • If the submissionPercent is 20.238, I need to return 20.24.

But submissionPercent should be a number not a String. If I use toFixed or toPrecision, then what I get is a String.

If possible, how do I achieve this?

10
  • 2
    20.00 is just the same number as 20. Commented Feb 11, 2014 at 19:51
  • No 20.12 would just remain as-is Commented Feb 11, 2014 at 19:52
  • 2
    someNumber = +(someNumber).toFixed(2); Source Commented Feb 11, 2014 at 19:53
  • 1
    You need to differentiate between formatting a number and storing a number. The two are quite different. Commented Feb 11, 2014 at 19:53
  • @arahant , ik , i was explaining to Bergi Commented Feb 11, 2014 at 19:53

2 Answers 2

5
var n = 20.238;

Math.round(n * 100) / 100
// => 20.24

Or more generally:

function roundWithPrecision(num, precision) {
  var multiplier = Math.pow(10, precision);
  return Math.round( num * multiplier ) / multiplier;
}

roundWithPrecision(20.238, 2)
// => 20.24

roundWithPrecision(20.238, 1)
// => 20.2

As others have pointed out, 20 and 20.00 are exactly the same, so if you want the user to see two digits after the decimal point even if they're zero, you'll have to use a string formatting function like toFixed.

In the case of a JSON API, it's up to the consumer to decide how to store the value upon decoding it. Even if you were to output JSON that said { val: 20.00 } a consumer will store it with whatever width it chooses. It might end up in a 32-bit float or a 64-bit float. It might end up in a string. It might end up in a MySQL database in a VARCHAR(6) column. You can't control how the consumer treats your data, and this is by design.

There is no advantage to outputting { val: 20.00 } over { val: 20 } or { val: 20.00000 } or { val: 2000e-2 }. If you want the API consumer to assume two digits of precision, you should put that information in your API documentation. I would encourage this, as it will help other developers make decisions when implementing API clients.

Sign up to request clarification or add additional context in comments.

8 Comments

yes. but for 20 it gives me 20 not 20.00, as I was hoping was possible.
Note: multiplying, rounding and then dividing reduces the usable numeric range, if you require large precision numbers.
"20.00" is only possible format as a String, which is what @Bergi was trying to explain.
@arahant In what context? In every sense, 20 is 20.00 and vice versa. var num = 20 is exactly the same as var num = 20.00
@arahant In the case of a JSON API the consumer will decide how to represent the number internally. This is by design. If you want to ensure that a developer writing an API client knows to expect non-integer values you should include that information in your API documentation.
|
1

you can use a combination of parseFloat and .toFixed() method;


const someNumberWithPrecesion = 123.1600001;

const newNumber = parseFloat(someNumberWithPrecesion.toFixed(3));

console.log(newNumber) // 123.16
console.log(typeof newNumber) // "number"


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.