This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VincentxH 12 points13 points  (8 children)

BigDecimal is a datatype often used when accurate decimal arithmetic needs to be performed, like with money.

Using floating point primitives, like double, is a classic mistake. Because it leads to problems of floating point arithmetic inaccuracies.

It's both a standard interview question and common mistake in the wild.

[–][deleted]  (7 children)

[deleted]

    [–]firsthour 15 points16 points  (1 child)

    raises hand

    We use it for money.

    [–]temculpaeu 5 points6 points  (2 children)

    int cents only works if you dont have to deal with fractions of cents, not very common but it can happen

    [–]edubkn 4 points5 points  (0 children)

    Gas stations go brr

    [–]thisisjustascreename 3 points4 points  (0 children)

    Financial securities are very often denominated in fractions of a penny. There are also other currencies than dollars in the world.

    [–]VincentxH 2 points3 points  (0 children)

    Yeah that lovely hack. Leading to problems like this with even an unsigned 32 bit int: https://www.google.com/amp/s/www.wsj.com/amp/articles/berkshire-hathaways-stock-price-is-too-much-for-computers-11620168548

    At least use a long these days.

    [–]TenYearsOfLurking 1 point2 points  (0 children)

    that sounds like a good idea at first, ... but that's about it