Take the 2-minute tour ×

It feels like I am missing something obvious here. This has been asked a number of times - and the answer usually boils down to:

var num = 4.5;
num % 1 === 0; // false - 4.5 is a decimal

But, this fails for

var num = 1.0; // or 2.0, 3.0, ...
num % 1 // 0

Unfortunately, these doesn't work either

num.toString() // 1
typeof num // "number"

I am writing a JavaScript color parsing library, and I want to process input differently if it is given as 1.0 or 1. In my case, 1.0 really means 100% and 1 means 1.

Otherwise, both rgb 1 1 1 and rgb 1 255 255 will be parsed as rgb 255 255 255 (since I am right now taking anything <= 1 to mean a ratio).

share|improve this question
So, if there's a decimal point, it's a percentage, otherwise it's just the integer value? –  Jonathan M Sep 14 '11 at 19:46
I think you're out of luck since 1.0 === 1. –  pimvdb Sep 14 '11 at 19:46
Short of getting it as a string, I don't think you have any options here. JavaScript does not make any real distinction between floats and integers. I'll be following the question though, I'm interested to see if there's a potential answer. –  Alex Turpin Sep 14 '11 at 19:47
Jonathan - yes, that is what I am hoping to do. –  Brian Grinstead Sep 15 '11 at 14:08
add comment

5 Answers

up vote 2 down vote accepted

Those numbers aren't actually decimals or integers. They're all floats. The only real difference between 1 and 1.0 is the notation that was used to create floats of equal values.

Edit: to help illustrate, consider:

1 === 1.0; // true
parseInt('1') == parseInt('1.0'); // true
parseFloat('1') === parseFloat('1.0'); // true
parseInt('1') === parseFloat('1'); // true
// etc...

Also, to demonstrate that they are really the same underlying data type:

typeof(1); // 'number'
typeof(1.0); // 'number'

Also, note that 'number' isn't unambiguous in JavaScript like it would be in other languages, because numbers are always floats.

Edit 2: One more addition, since it's relevant. To the best of my knowledge, the only context in JavaScript in which you actually have "real and true" integers that aren't really represented as floats, is when you're doing bitwise operations. However, in this case, the interpreter converts all the floats to integers, performs the operation, and then converts the result back to a float before control is returned. Not totally pertinent to this question, but it helps to have a good understanding of Number handling in JS in general.

share|improve this answer
Thanks for the detailed response - I have been learning a lot about how JS handles numbers from this project. I have a feeling I'm going to need to only accept string input for "1.0" in an object. So, tinycolor({r: .5, g: "1.0", b: .9}). –  Brian Grinstead Sep 15 '11 at 14:15
You could do it that way, or perhaps refactor the API to be a little more explicit. E.g., tinyColor(/*assumes 0-255*/) and tinyColor.fromRatio(/*assumes decimals*/), etc. –  jmar777 Sep 15 '11 at 15:23
Hmm that is a good thought. My goal is to have the string parsing as robust as possible, where the 1.0 and 1 difference can be detected. I am using this library for a colorpicker, and I want the user input to be as permissive as possible - you can type 'red', 'f00', 'ff0000', 'rgb 255 0 0', 'rgb(255, 0, 0)', 'rgb 1.0 0 0', etc. I suppose if there needs to be another method for object input that specifically asks for ratios that could work, but I'm leaning towards just requiring a string, since it gets really weird with: { h:360, s: 1.0, v: 1.0 } :) –  Brian Grinstead Sep 15 '11 at 16:10
Well if this is for user input, then I think you're golden with the string parsing, since that's how you'll actually get the user-provided value, presumably. I think it would be awkward if it was a code API, but if you're controlling things from text input and on, then string parsing seems legit. –  jmar777 Sep 15 '11 at 16:13
Yeah, I will just modify my bound01() function here: github.com/bgrins/TinyColor/blob/… to allow "1.0" string input. I think I will just treat "1.0" as "100%". This seems like the best solution, though I still wish I could get the difference between 1.0 and 1 –  Brian Grinstead Sep 15 '11 at 16:25
add comment

You'll have to do it when parsing the string. If there's a decimal point in the string, treat it as percentage, otherwise it's just the integer value.

So, e.g.:

rgb 1 1 1   // same as #010101
rgb 1.0 1 1 // same as #ff0101

Since the rgb is there, you're parsing the string anyway. Just look for . in there as you're doing it.

share|improve this answer
I might need to do it that way. I would still like to be able to handle input as an object though, since I allow both 'rgb 1 1 1' and {r: 1, g: 1.0, b: 1} –  Brian Grinstead Sep 15 '11 at 14:10
To do that, detect the typeof variable passed. If object, treat appropriately; if string, parse. The trick will be with the object, since the values for the attributes will be floats. As discussed in other answers, once you're in a float, you won't be able to tell if an integer was initially expressed with a decimal point or not. You might do best to require the user to express percentages with a % sign. This is, of course, forcing them to be strings. –  Jonathan M Sep 15 '11 at 14:12
Thanks Jonathan. After all the responses, I think I will handle the string parsing as described, and force the object input to either be in percentages, or as a string for the special 1.0 case { r: "1.0", g: .5, b: .5 }. –  Brian Grinstead Sep 15 '11 at 16:16
add comment

Let your script parse the input as string, then it will be a matter of checking if there is the point like this.


Check this example and this example.

share|improve this answer
Thanks, but 1.0.toString() turns into "1" –  Brian Grinstead Sep 15 '11 at 14:14
Check this example please. If you input a int number, it will return false, if you type 1.0 for example, it will return true, effectively checking it's a float. –  Jose Faeti Sep 15 '11 at 14:31
You are right about that, and I will implement it like that IF the input is a string. The tricky part is if an object is passed in, like { r: 1.0, g: 1, b: .5 }. It seems once the variable is in memory, there is no difference between 1 and 1.0. –  Brian Grinstead Sep 15 '11 at 16:05
Once a float is in memory, the decimal part gets removed automatically if equals zero. From there on you cannot possibly know if the was an integer or a float. –  Jose Faeti Sep 15 '11 at 16:33
add comment

Well, as far as the compiler is concerned, there is no difference between 1.0 and 1, and because there is no difference, it is impossible to tell the difference between them. You should change it from 1.0 to 100 for the the percentage thing. That might fix it.

share|improve this answer
add comment
var num = 1;


var num = 1.0;

are the same. You mention that you want to treat them differently when given from a user. You will want to parse the difference when it is still a string and convert it to the appropriate number.

share|improve this answer
add comment

Your Answer


