0

The goal is to isolate the decimal value (with simply turning the integer into a string and splitting it).

To that affect I have:

var x = 1001.1;
var roundedDown = Math.floor(x); // Produces 1001
var decimal = x - roundedDown;

The expected value of decimal should be 0.1 (1001.1 - 1001). But instead I get 0.0999999 recurring.

I have tried with Math.trunc as well but get the same results.

Would anyone know why this is?

MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • 1
    There is no such thing as "integer with a decimal". It's like "vegetarian goulash with pork". – Amadan Aug 14 '19 at 04:43
  • What's the correct term, just out of interest? Simply integer? – MeltingDog Aug 14 '19 at 04:47
  • From mathematical perspective, a [real number](https://en.wikipedia.org/wiki/Real_number). From a computing perspective, a [floating-point number](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Floating-point_numbers) ("float" for short). Integers (both [mathematically](https://en.wikipedia.org/wiki/Integer) and [computationally](https://en.wikipedia.org/wiki/Integer_(computer_science))), by definition, can't have a [fractional part](https://en.wikipedia.org/wiki/Fractional_part). – Amadan Aug 14 '19 at 04:48

0 Answers0