0

I have searched through a lot of threads and all of them give the same answer.

I am trying to add 0s right after the decimal point to an integer. If I have var a = 25. I want to modify a such that its value is 25.0. Most of the offered solutions either suggest appending a string ".0" or using .toFixed() which appends any number of 0s you want but still returns a string. I want the response to be float number and not a string.

Any thoughts on how I could achieve this?

Thanks.

dev
  • 11
  • 1
    possible duplicate of [javascript convert int to float](http://stackoverflow.com/questions/4057489/javascript-convert-int-to-float) – OnlyThenDidIReckonMyCurse Jan 14 '14 at 21:59
  • 9
    Float numbers don't store data like that (or any number type for that matter). Such visual things must be done with a string. – Nathan Merrill Jan 14 '14 at 21:59
  • 3
    You can't achieve this, that is why all the answer you've found uses a string. – adeneo Jan 14 '14 at 21:59
  • That has to be done as a string. The number is 25, wether it's a float, int, decimal, whatever. To include trailing 0s it must be a string (which you do for display, not for storing it in your object). – CaffGeek Jan 14 '14 at 22:00
  • The number representation is exactly 25. There are no more significant digits and so nothing else is displayed. – Travis J Jan 14 '14 at 22:00
  • 1
    Javascript doesn't have `int` and `float`. It has only `number`. – Matt Burland Jan 14 '14 at 22:00
  • @MattBurland - You're sure it's not `Number`, but it still has something resembling integers and floats – adeneo Jan 14 '14 at 22:01
  • @adeneo: what I mean is `typeof(4)` returns `number` and `typeof(4.5)` also returns `number`. Now there's also the build-in type `Number`, but I've never found a reason to use it. – Matt Burland Jan 14 '14 at 22:04
  • @adeneo - It is [Number](http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19): "primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value." – Travis J Jan 14 '14 at 22:04
  • Thanks for the responses guys. I understand there is no int or float in JS i.e. it has no specific data type declaration. I am calling a service from my front end code which expects the input to be in the form of a floating point number with a trailing 0 if it doesn't have a decimal part because for numbers with digits after the decimal point JS stores them as a number and not a string. Looks like I will have to find a way of passing in a string and then converting it on the server side. – dev Jan 14 '14 at 22:08
  • @MattBurland - All types returned by `typeof` are lower case. Not sure why. http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3 – Travis J Jan 14 '14 at 22:09
  • @dev: If your service demands a trailing 0 then it's not really expecting a `float` since `25` is a perfectly valid float. So it sounds like the problem is on your service. Pretty much any language would likely send `25.0` as `25`. – Matt Burland Jan 14 '14 at 22:10

1 Answers1

0

It has been a while since someone used this thread, but just in case someone needs it I think the answer you are looking for is:

.toFixed(2)

Example:

let number = 4;
return number.toFixed(2) //4.00