# Get decimal portion of a number with JavaScript – Dev

The best answers to the question “Get decimal portion of a number with JavaScript” in the category Dev.

QUESTION:

I have float numbers like `3.2` and `1.6`.

I need to separate the number into the integer and decimal part. For example, a value of `3.2` would be split into two numbers, i.e. `3` and `0.2`

Getting the integer portion is easy:

``````n = Math.floor(n);
``````

But I am having trouble getting the decimal portion.
I have tried this:

``````remainder = n % 2; //obtem a parte decimal do rating
``````

But it does not always work correctly.

The previous code has the following output:

``````n = 3.1 // gives remainder = 1.1
``````

What I am missing here?

``````var decimal = n - Math.floor(n)
``````

Although this won’t work for minus numbers so we might have to do

``````n = Math.abs(n); // Change to positive
var decimal = n - Math.floor(n)
``````

Use `1`, not `2`.

``````js> 2.3 % 1
0.2999999999999998
``````

How is 0.2999999999999998 an acceptable answer? If I were the asker I would want an answer of .3. What we have here is false precision, and my experiments with floor, %, etc indicate that Javascript is fond of false precision for these operations. So I think the answers that are using conversion to string are on the right track.

I would do this:

``````var decPart = (n+"").split(".");
``````

Specifically, I was using 100233.1 and I wanted the answer “.1”.

``````n = (n + "").split(".");