1

I was bored and was playing with angular filters, and I've noticed that angularjs will change the number if you use | number filter, which shouldn't affect my number in anyway, but somehow if I filter 9999999999999999 it will change it to 10000000000000000, while if I filter 9999999999999998 it will leave it as it is 9999999999999998.

Plunker example

I am very curious and looking forward to know, what makes javascript/angularjs filter to change my number by 1 unit?

rossanmol
  • 1,633
  • 3
  • 17
  • 34
  • 3
    That is actually JS problem, try `var n = 9999999999999999;` – IProblemFactory Dec 23 '16 at 14:09
  • 1
    Welcome to floating point numbers. – phuzi Dec 23 '16 at 14:10
  • 2
    Javascript only has 64-bit floats - and you've ran out of floating-point precision. – Alex Szabo Dec 23 '16 at 14:10
  • JavaScript really got your number on that one! – Jeroen Dec 23 '16 at 14:10
  • 1
    Why this happens? If a bank will use javascript, it could bring a bug to the whole system. – rossanmol Dec 23 '16 at 14:10
  • @PHPLover people who code with really big (and really small) numbers will run in to this type of problem quite often and there are libraries for most, if not all, languages to deal with these issues – phuzi Dec 23 '16 at 14:14
  • Good js practices for money and other floating points where precision needs to be exact, is to not use floating point numbers, but multiply/divide everything back to an integer before doing maths. For numbers bigger than the limit, you can use strings and arrays to represent really big numbers. Writing a module to do maths with arrays is quite trival. – Shilly Dec 23 '16 at 14:26

1 Answers1

1

That is actually JS problem, try var n = 9999999999999999; and it is related to: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66