0

I have a JSON-like input format:

{
    "a": 2,
    "b": 0.0,
    "c": "hi"
}

There are Float values (always with decimal point) and Integer values (no decimal point). I need to parse it with Javascript, work with it and then write it back into file (floats with decimal point).

It is straight-forward to parse it into classic object, but I need to distinguish between Floats and Integers when encoding it back. I currently use this ugly JS structure:

{
    type: "Object",
    value: {
        a: {type: "Integer", value: 2},
        b: {type: "Float"  , value: 0},
        c: {type: "String" , value: "hi"}
    }
}

I want to simplify my structure. Is there any way to distinguish Floats and Integers inside JS Numbers? Like "free bit" or something? Or maybe some smart trick?

E.g. if all my input numbers are always positive, I can store Integers as positive numbers, Floats as negative numbers. But they are not :( any other idea?

Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46
  • 1
    possible duplicate of [How to check if a number is float or integer?](http://stackoverflow.com/questions/3885817/how-to-check-if-a-number-is-float-or-integer) – edhedges Aug 16 '14 at 15:10
  • I think this is the best you can do considering it is part of the JSON spec (http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example) – Mark Silverberg Aug 16 '14 at 15:11
  • I do not need to check anything. I need to put one bit of information (float or integer) into number (when parsing) and read it later (when encoding) – Ivan Kuckir Aug 16 '14 at 15:13
  • My Input format looks like JSON, but it is not JSON, I do not work with any JSON. – Ivan Kuckir Aug 16 '14 at 15:14
  • 2
    Something like what you're already doing is probably the simplest approach. – Pointy Aug 16 '14 at 15:15
  • If you want to preserve `0.0` from your JSON-like format as float instead of `0` int, then there is no other way than using a separate field to denote it during processing since custom properties on `number`s disappear. – Artjom B. Aug 16 '14 at 15:23

3 Answers3

1

In JavaScript, all numbers are internally expressed in 64-bit floating point form. There simply is no such thing in JavaScript as a float or integer, there are just Number's. So if you want to pass information to another system about whether this is intended to be a float or integer, you have to encode that elsewhere, similar to the structure you already have.

It's not possible in JavaScript to detect whether a numeric literal was expressed with a fractional part or not. The closest you can get is to test whether a number has a fractional part. This is easy to do with the modulo operator (%):

2 % 1 === 0
> true

2.0 % 1 === 0
> false

However,this still can't distinguish numeric forms like 0.0 from 0:

0 % 1 === 0
> true

0.0 % 1 === 0
> true

If you're numbers are in fact stored as strings, you could simply test for the . character:

"0".indexOf(".") === -1
> true

"0.0".indexOf(".") === -1
> false
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Storing numbers in strings is a great idea! :) But sadly, my input format already has String type ... But maybe I can use additional character at the beginning. – Ivan Kuckir Aug 16 '14 at 15:18
  • @IvanKuckir It sounds as though you're writing a custom parser. If that's the case, can you simply break from JavaScript's handling of numeric literals and treat `0` and `0.0` as different types of value when you parse the input? – p.s.w.g Aug 16 '14 at 15:22
0

You could use parseInt or the modulo operator and do some fancy tricks like

if (parseInt(n) === n)
    console.log("Integer");
else
    console.log("Float");

or

if (n % 1 === 0)
    console.log("Integer");
else
    console.log("Float");
lvogel
  • 1,278
  • 1
  • 7
  • 13
0

I finally decided to keep numbers as strings and use first character to distinguish them (i:integer, f:float, s:string). I will have to do additional parsing when accessing values.

{
    "a": "i2",
    "b": "f0",
    "c": "shi"
}
Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46