0

I would like to return the number, 15.00, in a json payload: {"theNumber" : 15.00}. All my research on SO has suggested to do something like parseFloat(15).toFixed(2), but this always returns a string and therefore does not solve the problem of returning 15.00 as a number / float.

Is it possible to return a number in JS with two decimal values as zeros?

robskrob
  • 2,720
  • 4
  • 31
  • 58
  • No. Numeric types get auto-formatted. The zeroes don't matter in that case. If you want to display something, use a string. – VLAZ Nov 11 '16 at 21:29
  • where would you like to display it? – Nina Scholz Nov 11 '16 at 21:31
  • @NinaScholz I would like to respond with json, and I want this json to contain a key value - `theNumber: 15.00` -- i'll update the question to have this detail – robskrob Nov 11 '16 at 21:32
  • `parseFloat(parseFloat(15).toFixed(2))` – Ergec Nov 11 '16 at 21:33
  • 1
    `15 === 15.00`. If you want to *display* a number with two decimal points, it *must* be a string. – Mike Cluck Nov 11 '16 at 21:33
  • @robertjewell why does it matter how many zeroes does a number have? Again, if you want to display something, use a string, if you don't care (with JSON, you're generally just transferring information, so no users need to see it), then use the number as is. – VLAZ Nov 11 '16 at 21:33
  • 1
    This is not possible. See here: http://stackoverflow.com/questions/19938008/trailing-zeros-in-javascript – gus27 Nov 11 '16 at 21:34
  • 4
    @Ergec why do you thing that will do anything? – VLAZ Nov 11 '16 at 21:34
  • @vlaz it matters because what should front ends expect -- a number or an integer? – robskrob Nov 11 '16 at 21:35
  • 2
    Then format it on the front end. I really don't see the issue here. – VLAZ Nov 11 '16 at 21:36
  • @vlaz I have tests that expect an integer but some times parseFloat returns a float. So when it's a float it breaks, but when its an integer it passes. – robskrob Nov 11 '16 at 21:37
  • 2
    You realize that integers are numbers? –  Nov 11 '16 at 21:37
  • Yes I use `+(parseFloat(15).toFixed(2))` – Stephen Couturier Nov 11 '16 at 21:38
  • This is not a duplicate of that question. –  Nov 11 '16 at 21:38
  • @Amy yeah I'm learning the word choice of the JS community. Basically I want parseFloat to do what it suggests -- turn `15` into a float. – robskrob Nov 11 '16 at 21:39
  • @robertjewell integers are indeed numbers as Amy says. If you want an integer, then convert it to that. If you want a float, then convert it to that. Yes, a float CAN be an integer in JS, since technically neither exists - JS only has a single numeric type. – VLAZ Nov 11 '16 at 21:39
  • @robertjewell **ALL** numbers are the same type: float. If you want to *display* it a certain way, you do that with a string. – Mike Cluck Nov 11 '16 at 21:39
  • @robertjewell there aren't different types of numbers in JS. The difference between the integer `15` and the float `15.0` is literally nonexistent. They are the same number. Hence why JS drops the zeroes. – VLAZ Nov 11 '16 at 21:41
  • @gus27 has the correct duplicate in their comment, namely [Trailing zeros in javascript](http://stackoverflow.com/q/19938008/215552). – Heretic Monkey Nov 11 '16 at 21:41
  • 15.00 inside a JSON object is just a value. And for javascript 15 and 15.00 are number literals and will have the same value for a number type. – Ali Naci Erdem Nov 11 '16 at 21:43

0 Answers0