## How to Round to Arbitrary Precision in any Programming Language

How to Round to Arbitrary Precision in any Programming Language: an AMultiply your rounding number by 10^y where y is the precision to round to. Call your language’s built-in round() method on this number. Divide the result by 10^y. Easy peasy. It also makes it easier to implement different rounding methods because you’re always dealing with the digit at the same location in the number.

How to Round to Arbitrary Precision in any Programming Language: Multiply your rounding number by 10^y where y is the precision to round to. Call your language’s built-in round() method on this number. Divide the result by 10^y. Easy peasy. It also makes it easier to implement different rounding methods because you’re always dealing with the digit at the same location in the number.

In javascript:

Then there’s Math.round() which you may not have or may not want to use and, if you’re rounding, at all, you’ll inevitably find yourself looking at various rounding methods. Since Math.round(), and usually all default round() methods in most programming languages, use traditional “half up” rounding, you may find you need a different method, like banker’s rounding which is “half to even”.

To accomplish a different rounding method, create your own round() function, apply the appropriate power to your number. Then, subtract the integer portion of the number. You are left with the fraction portion of your number: This gives you the rounding digit as your final number. With this number, you can then decide whether to truncate the original number or add 1. Once you’re done that, you can then remove the power manipulation you did in the first step and now you’re left with the final rounded number.

In javascript:

The functions Math.floor() (floor and ceiling), Math.pow() (exponentiation), and the modulus operator, are usually already available for most modern languages, under the same names, and don’t really have a need to be re-implemented compared to the need to re-implement rounding. Implementing your own functions for these is usually relatively trivial, however.

There you go! Arbitrary precision rounding in any programming language and, thrown in for good measure, how to implement a different rounding method to boot!

## The right tool for the right job: Not so simple.

The right tool for the right job, like most things in life, is more complex, more difficult to understand, and takes effort to grasp the reason and benefits of its true meaning.

The argument “the right tool for the right job” is as old as they come. It’s similar in spirit to the old adage that you can’t put a “square peg in a round hole“.

The problem is no scenario is black and white.

You’re on Microsoft Windows so you should use .NET? You’re on Mac so you should use Objective-C and Cocoa? You’re on linux so you should use C and GTK?

The right tool for the right job is not just about price/performance ratios, the primary goal of a language, or what a language has tradionally been used for.

You use a programming language for a task because you’re an expert in that language and you can bend it to your will with greater ease than implementing in a new language.

Business understands this. It’s about efficiency not “perl is for data” and “python is for prototyping” and “C is for algorithms” and “java is for apps.”

It’s not black and white.

Microsoft Office might, in a very base sense, be the best tool for the job if you’re dealing with Microsoft Office format files. But the “right tool for the right job” includes conditions like price, licensing, security risk, training, etc.

Licensing is a big issue. The internet and the FOSS movement, from which we all benefit enormously today, was built on open standards, open protocols and open code.

Stallman understands that we’re where we are today because IT pioneers simply found it easier, better and more fulfilling to craft open source and have all modifications on open source returned back to the source.

We have a great computer ecosystem because the right-tool-right-job mentality did not include the idea that one should go with the status quo which is so often the case when people bring up this argument.

The right tool for the right job, like most things in life, is more complex, more difficult to understand, and takes effort to grasp the reason and benefits of its true meaning.