0

In JavaScript, in order to know whether a number is an integer or not, we can directly use the method

Number.isInteger(10); //return true
Number.isInteger(20); //return true
Number.isInteger(20.10); //return false

However, if I pass 1.0 in it doesn't return me what I am expecting.

Number.isInteger(1.0); //return true

I want to treat 1.0 as floating point number and not a integer number. Is there any way in JavaScript to differentiate between 1 as int and 1.0 as float?

Ashvin777
  • 1,446
  • 13
  • 19
  • check if number contains decimal or not. – RK_15 Mar 14 '19 at 09:19
  • Why do you need this? There is not something like an integer in javascript, all the numbers are stored as double (yes, also not decimal ones!). But why do you need 1.0 to be considered a float? – Christian Vincenzo Traina Mar 14 '19 at 09:19
  • @RK_15 he can't check if the number contains decimals, because when you convert `1.0` to string, you get `"1"` – Christian Vincenzo Traina Mar 14 '19 at 09:20
  • I am writing a JSON schema generator tool which has to treat this type of data as float type. – Ashvin777 Mar 14 '19 at 09:23
  • Is the schema inter-language? If it is, then you can add a property `"type": "float"` at the same level. If it is javascript only, then you should just not care – Christian Vincenzo Traina Mar 14 '19 at 09:24
  • That is an inter language schema. – Ashvin777 Mar 14 '19 at 09:25
  • Can't you pass the numbers as strings and then check for decimal in those. – RK_15 Mar 14 '19 at 09:25
  • If we convert `(1.0).toString()` it return us `"1"` and not `"1.0"`, so we can't check dot in the stirng. – Ashvin777 Mar 14 '19 at 09:26
  • @Ashvin777 I am talking about "1.0".indexOf(".") !== -1. So here I am passing "1.0" not 1.0. – RK_15 Mar 14 '19 at 09:27
  • Well, that si definitly possible incase when the value is in the format of string and not in number. In my case I have a number `1.0` in number format only – Ashvin777 Mar 14 '19 at 09:28
  • you can't have a number in any variable in 1.0 format. If you r getting that number from JSON string then also it will be in number format. 1.0 as number you can't hold as it is, as soon as you will assign 1.0 to any variable or object property it will be changed into 1. – RK_15 Mar 14 '19 at 09:31
  • No, you don't have `1.0`, you have `1`, as the answer in the linked question says! I advise you the only solution: use `Number.isInteger` to make the first prediction. If it's a schema, then you will have several cases using it. If there is at least a float, change the wrong prediction. After all, that's just an induction – Christian Vincenzo Traina Mar 14 '19 at 09:31
  • I think the best possible solution for this would be to change the `input` type to `text`, so that it will return me values in text format, that way I will be able to add both checks `isInteger` and `doesHaveDecimalInString` – Ashvin777 Mar 14 '19 at 09:34
  • You have a "Real" problem. In mathematics 1.0 belongs to both **R** and **I** and is an integer. So what is the problem you are trying to solve. – traktor Mar 14 '19 at 09:37
  • I voted to reopen this ticket, as the answer on the other ticket is explains the reason behind this behavior but doesn't provide the solution. If you guys can help in reopening this it will be great. It is currently marked duplicate. – Ashvin777 Mar 14 '19 at 09:39
  • 1
    So the answer to your question is no. JavaScript holds numbers in floating point format. You can differentiate between string values "1" and "1.0", but not between them when provided at compile time as numbers. They are the same number after all. – traktor Mar 14 '19 at 09:47

0 Answers0