I’m puzzled as to what this example purports to show. Multiplying a number with 2 digits after the decimal point by another number with two digits after the decimal point produces a number with four digits after the decimal point. For example, 0.71 by 1.06 produces 0.7526. If you do not take that into account you don’t get the correct result, if it is taken into account — it works. There is no need to run this code to see that.

Hint: I know how BigDecimal works internally, since, as a kid, I grew up in pre-Java times where you did not have arbitrary precision arithmetics in your standard library and I was trained to know all the algorithms for all the “long arithmetics” (as it was colloquially called) by heart.

There is no point in restating the fact that using BigDecimal it is easier to arrive at the correct result. It indeed is. I did not claim otherwise. The standard support of BigDecimal in Java is a great progress and we will definitely have BigDecimal supported one day in both Kotlin/JS and Kotlin/Native.

Written by

Project Lead for the Kotlin Programming Language @JetBrains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store