Adrian Tache
Jun 28, 2019 · 1 min read

The Java tutorials mention this and I highly recommend going through them. In particular, this is mentioned for the float and double types:

This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

The BigDecimal documentation is also interesting, they explain why float and double have rounding errors.

Of course, I feel just using cents or tenths of cents is a much simpler solution, as is simply rounding values, not like you’ll miss that $0.000003.

    Adrian Tache

    Written by

    Mobile Developer 📱

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade