-2

I want to convert 1 to 1.0.

I'm aware of the .toFixed() method however this converts it to a string. I need it to be an integer.

How do I do this?

EDIT: I'm just going to request back-end to sort this out as there should be no problem posting 4 instead of 4.0 as they are the same number.

Thanks for the help everyone.

Malcr001
  • 8,179
  • 9
  • 44
  • 57
  • 1
    `1` is an integer already :) – akonsu Jul 23 '13 at 13:33
  • Yes it is but how do I turn it into 1.0? – Malcr001 Jul 23 '13 at 13:34
  • 1
    Back-end requires it to be an integer in this format, thats why. – Malcr001 Jul 23 '13 at 13:35
  • Back-end will _always_ receive the number as a string, and (as I stated in my answer) JS _has no `float` type_, only `Number` – Elias Van Ootegem Jul 23 '13 at 13:36
  • Transmission is always a string – Esailija Jul 23 '13 at 13:36
  • Not possible, consider this: `alert (1.0);`, it alerts 1 – NibblyPig Jul 23 '13 at 13:36
  • Seriously, you really need to give more information. How are you transmitting the data? What encoding? What format? –  Jul 23 '13 at 13:46
  • The data is sent as a JSONPost so I do not believe integers are treated as strings. – Malcr001 Jul 23 '13 at 14:02
  • You're right. In JSON, the numeric type can have a decimal. Unfortunately, JavaScript doesn't differentiate the types, so it'll always remove the decimal for whole numbers. I think you'd need to do your own JSON serializing to get the numeric representation you want. –  Jul 23 '13 at 14:06
  • @user971824: You should probably update your question so that people don't keep leaving unhelpful answers. –  Jul 23 '13 at 14:10
  • I'm running into this exact issue right now. This is NOT a bad question. Just because you haven't encountered it yet... :) – bob Oct 31 '19 at 18:08

3 Answers3

3

Integers only exist in transient situations in JavaScript. You can force one by using some no-op combination of bitwise operators:

var x = 22.3;
x = ~~x;

That will result in "x" being 22, but it's really still a floating-point value. The integer value existed during the expression evaluation of ~~x, but when the value is stored it's back to floating point. (Sorry I edited out the bogus expression. edit no maybe it was OK; still on 1st cup of coffee ...)

Note that ~~ is two applications of the bitwise "not" operation, which will leave the (integer) value of the original number.

edit — as to your comments about posting the value to your back-end code, understand that the HTTP parameters are essentially strings anyway; that is, the numeric value is rendered as a string of decimal digits (and possibly decimal point, sign, exponent, etc). Thus, for that purpose, .toFixed() is as good as anything else. Once the HTTP parameters reach the server, it's up to code there to interpret the parameter value as a number.

edit again — If you're posting a JSON object, then it's still the case that everything is a string. You should be using a JSON encoder to create the serialized version, and it'll leave off fractional parts of numbers that have none. You can write your own serializer, but before doing that you'd be better off spending some time figuring out what's wrong with your server-side JSON parser that keeps it from recognizing valid numbers without decimal fractions.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I'm guessing OP is sending as JSON, and doesn't want the JSON's string data type. In other words, the decimal formatted number without the quotes, which is valid JSON. `{"foo":42.0}` Whereas if OP used `.toFixed()`, it would end up as `{"foo":"42.0"}`. Would be nice if OP would let us know though. –  Jul 23 '13 at 14:00
  • @CrazyTrain yes, the OP comments that it's JSON. Of course, it's probably best if the OP uses a standard JSON serialization tool, and those won't include a fractional part for numbers that are (in value) integers. – Pointy Jul 23 '13 at 14:07
  • Crazy Train that is correct I am send sending the data as a JSON post object. – Malcr001 Jul 23 '13 at 14:07
  • @user971824 your question is confusing. Perhaps you could explain why it is that your server-side code insists on numbers having a decimal fraction part when none is necessary. – Pointy Jul 23 '13 at 14:09
  • @Pointy: Yeah, that seems to be the problem. The back end seems to require the decimal. –  Jul 23 '13 at 14:09
  • @CrazyTrain so what respectable JSON parser would be thusly broken? – Pointy Jul 23 '13 at 14:10
  • I agree. I will request back-end to change this as the server side should not have a problem with 4 instead of 4.0. – Malcr001 Jul 23 '13 at 14:13
  • @Pointy I doubt the back-end parser is broken. More likely a library in a statically typed language that is requiring a `float`, while the parser is creating an `int`. Would seem like they could loosen up a bit. –  Jul 23 '13 at 14:13
  • ...oh, and I actually missed your first boolean/bitwise switch. The one I saw was *"Note that `~~` is two applications of the boolean "not" operation..."*. Couldn't figure out why you weren't changing it. I'm on my first cup as well! :-) –  Jul 23 '13 at 14:17
0

In JavaScript you don't have separation between integers and floats. There are only Numbers. So whenever you save your 1 in a var, he is actually 1.0. To represent it on the screen you have to use .toFixed(). It's a string representation as you said. If you want to calculate something like 1 + 2.8 as a result you will have 3.8.

Dima Kuzmich
  • 1,286
  • 11
  • 18
-1

you can use (1).toFixed(1) and if you again want it as an integer then change it using parseFloat((1).toFixed(1));