Round to the nearest … What?!

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:

  1. Multiply by the decimal that you want to round to.
  2. Round.
  3. 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:

  1. The number that you wish to round, and
  2. 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!

Please, comment on and share this post with others if you enjoy it; follow @kodmunki on Twitter for regular updates on new developments, deployments, articles, and tools; check out kodmunki™ on Github for cool, managed, and tested kodmunki™ projects, and find heavy tunes to flip bits to on komdunki™ SoundCloud page.
Thank you for reading, and as always, happy coding :{)}
Advertisements

Software development leaders at the forefront of the latest in technology. Whether implementing updates or integrating with existing technology; developing products that push the bleeding edge of the latest in tech; or developing open-source products, paving the way of future tech, kodmunki™ inspires innovation, elevates quality, and drives value to production. kodmunki™ are experts in web, mobile, and hybrid solutions development; local and distributed team management and collaboration; and fast, quality, successful product delivery, offering R&D, training, consulting, and development services. Contact us at info@kodmunki.com. Let's discuss your vision.

Tagged with: ,
Posted in Uncategorized
One comment on “Round to the nearest … What?!
  1. […] 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 […]

Enjoy the read? -- Let us know.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

kodmunki™ Tweets

Enter your email address and receive notifications of new kodmunki™ posts by email.

%d bloggers like this: