0

I'm working with Typescript 2.4 and being stuck with a simple modulo operation :

I wrote a simple unit test of modulo operator to explain my problematic

fit('TypeScript should be able to do a simple mathematic modulo : ', () => {
  expect(318.08 % 60).toBe(18.08);
});

And as you can guess... this test fail (Expected 18.079999999999984 to be 18.08.).

What did i miss ?

As you wan see google is better as this game :D : https://www.google.fr/search?q=318.08+%25+60&oq=318.08+%25+60&gs_l=psy-ab.3...2056882.2060253.0.2061466.11.11.0.0.0.0.440.1441.2j2j1j1j1.7.0....0...1.1.64.psy-ab..4.5.1213...0j35i39k1j0i5i30k1.W8XFPAxxO44

Sid
  • 331
  • 2
  • 10

1 Answers1

0

This is not a typescript issue, it's a double precision issue. This is the value that is generated when using modulo on two doubles (checked in C# as well and I got the same value)

You could round the result to two decimal places

Math.round(100 *(318.08  % 60)) / 100
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357