I am scratching my head to find numbers like 10.0
, 1.0
are whether integers or floats. is there any way to check whether 10.0
is float or int in javascript ??
Thanks
Asked
Active
Viewed 3,172 times
4

REDDY PRASAD
- 1,309
- 2
- 14
- 29
-
1to answer your question, `'10.0'` is as number `10` and that is an integer value. you have to use string operations/regular expressions to check the **string** if you need to know zeroes after the dot. – Nina Scholz Jul 08 '18 at 07:50
-
1@Quentin and other mods, I nominated for reopening because technically this isn't a duplicate . This is specifically asking for checking for `.0` in JS floats – Oscar Godson Jul 08 '18 at 07:51
-
@Oscar — They aren't strings, they are number literals. `10.0 === 10` – Quentin Jul 08 '18 at 07:53
-
@Quentin correct, which is something we understand by OP didn't. 10.0 _is_ 10 which I mentioned in my answer. The question you marked it as a dupe with however is asking to check for any floats where is this asking specifically how to handle this. I mentioned it needs to be a string to do this. – Oscar Godson Jul 08 '18 at 07:55
-
@Quentin yes, i am specifically asking numbers ends with `.0` , i am not asking about if variable is int or float – REDDY PRASAD Jul 08 '18 at 07:55
-
1@REDDYPRASAD — You can't distinguish between `10` and `10.0`. As I said, they are the same – Quentin Jul 08 '18 at 07:56
-
@OscarGodson, I don't see any reason to reopen the question. But you may ask quentin to change the duplicate to [this](https://stackoverflow.com/q/280634). But as the question is an [XY problem](https://meta.stackexchange.com/q/66377) I would prefer I leave it as it is. – Munim Munna Jul 08 '18 at 11:14
1 Answers
2
You can check by dividing by 1
function isInt(n) {
return n % 1 === 0;
}
To expand a bit more as well, you can't ever have 10.0
in JavaScript. If you have var i = 10.0
and you log out i
it will return 10
. This is common across most (all?) programming languages.
If you really need to keep the .0
you need to store it as a string. You then can simply check the string for a .
like '10.0'.indexOf('.') > -1

Oscar Godson
- 31,662
- 41
- 121
- 201
-
3```1.0 % 1 === 0``` returns `true` . but `1.0` is not integer. Its float – REDDY PRASAD Jul 08 '18 at 07:47
-
why not check the number against it's integer value, like `n === Math.floor(n)`? – Nina Scholz Jul 08 '18 at 07:47
-
2@REDDYPRASAD added more to my answer. `1.0` doesn't actually exist in JS really. – Oscar Godson Jul 08 '18 at 07:50
-
@NinaScholz it won't work. ```Math.floor(1.0) === 1.0``` returns `true` – REDDY PRASAD Jul 08 '18 at 07:53
-
1as i wrote above under the question, a *number* with a dot and following zeroes is for javascript a number - and while javascript does not know integers, you couzld just check if you have an integer number. javascript uses for *all* number 64 bit floats. the only way, to check is to take a string and perform a check for dot and zeroes. – Nina Scholz Jul 08 '18 at 08:10
-
2Common across all languages? Strongly typed languages make very clear distinctions, not only between integers and floats, but between different subtypes (short signed integer, short unsigned integer, long integer...). But course JavaScript is loosely typed and it only has `Number` as numeric type. Thus it isn't entirely clear of what the question is really asking. – Álvaro González Jul 08 '18 at 08:37
-
in some languages, `10.0` denotes a float, but not in javascipt, bacause all numbers are floats. – Nina Scholz Jul 08 '18 at 09:12