You know what is a royal bummer? When the language that you are working with doesn’t support, or, at least, poorly supports, something that you believe it damn well should. Today, let’s look at one of these features in JavaScript: *rounding*.

I mean, sure, JavaScript “supports” rounding. Math.round, right?

Right. But, I mean, c’mon. You mean to tell me that rounding 1.223673928 to 1, and 1784993729 to 1784993729 is * support*?! — I don’t think so. What am I supposed to do if I want to, say, round to the nearest hundredth?

Well, that’s easy enough, right? Simply:

- Multiply by the decimal that you want to round to.
- Round.
- Then, divide by the same amount.

For example:

Math.round(45.677 * 100) / 100 == 45.68

Perfect! Problem solved! Right? — Not quite.

Not only is this a ridiculous amount of low level math operations that the developer is expected to write out every time they wish to round, there are actually some values in JavaScript that, due to floating point arithmetic, *will not round correctly when using this method!*

For example, take 555.555. If we want to round this value to the nearest hundredth. We would use our common method:

Math.round(555.555 * 100) / 100.

And we would, rightfully, expect the result to be 555.56, **but it isn’t! It is 555.55!**

Wait — What?!

I know, right? Unexpected. And though, admittedly, it’s possible, in some cases, that this is not a *huge* deal. There are certainly plenty of cases where this is *not* okay. For example, what if you were building a distributed retail app with a disconnected point-of-sale calculator? The price that your calculator tells the user and the price that it actually *is* will not be the same, and, though it is only a penny (in USD), that penny increase from one screen to the next is going to make you look like a swindler. Not exactly the impression you are going for when trying to retain customers.

Enter ku4js-kernel math functions!

If we, instead, use the ku4js-kernel $.math functions we will get the expected result:

$.math.round(555.555, -2) == 555.56

*Whew!*

But what is that “-2” all about?

Well, ku4js-kernel’s $.math round, roundDown, and roundUp functions take two arguments:

- The number that you wish to round, and
- The tens exponent that describes to which digit you wish to round to.

So, 0 will be to the ones, because 10^0 == 1 and 3 would be to the thousands, as 10^3 == 1000 and -2 would be to the hundredth, because 10^-2 = .01. More information can be found on this, and other ku4js-kernel methods in the documentation.

So, there you go! Rounding that is easy, expressive, and *correct*!

I hope that you enjoyed this short read, found it insightful, and find the ku4js-* libraries useful! Too, if you are actually looking to round money, I suggest taking a look at the ku4js-kernel money class. It not only rounds, but also takes care of numerous other nuances with money. More on this in Very Valuable Value Objects!

[…] you are unfamiliar with the ku4js-kernel $.math.round function. I recommend you read about it. It is cool! In short, it is a function that correctly rounds decimals to the nearest tens […]