Skip to content

Selah.

Pause, and calmly think on that.

Menu
  • About
  • Chords
  • Free Gift
  • Highlights
  • Links
  • Microposts
  • The New Covenant
  • The Small Web
  • Thoughts
    • Personal Thoughts
    • Famous Thoughts
    • Random Thoughts
Menu

How to Round to Arbitrary Precision in any Programming Language

Posted on November 1, 2010November 19, 2010 by Shovas

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:

function round(x, y) {
  var p = Math.pow(10, y));
  x *= p;
  x = Math.round(x);
  x /= p;
  return x;
}

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:

// x: number to round
// precision: Positive number indicates the position of
//            the rounding digit after the decimal place.
//            Negative number indicates the position of
//            the rounding digit before the decimal place.
function roundEven( x, precision ) {
  x *= Math.pow(10, precision);
  var originalX = x;
  x -= Math.floor(x);
  if ( x < 0.5 ) {
    x = Math.floor(x);
  } else if ( x > 0.5 ) {
    x = Math.floor(x + 1);
  } else {
    if ( Math.floor(originalX + 1) % 2 == 0 ) {
      x = Math.floor(originalX + 1);
    } else {
      x = Math.floor(originalX);
    }
  }
  x /= Math.pow(10, precision);
  return x;
}

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!

CommentCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Loading ...

No more microposts.

  • Claims of 3000-6000 deaths at residential schools may be overstated
  • The Twitter House Rules
  • A look at the kind of a culture it takes to rescue us from the fertility crisis
  • Noah’s Flood: The year long big picture. In the regular Creation for Kids article in the fun and faith-building Creation Magazine.
  • “And Adam was not the one deceived; it was the woman who was deceived…” 1 Timothy 2:14
  • The most important thing the right could watch right now
  • The decline of Canada starts with Pierre Trudeau in the late 60s
  • GOG Launcher – A Retro Wrap Modern Experience For Classic Video Games
  • Getting Started On The Small Web
  • The Tolkien Iceberg

bible book canada centos christ christian christmas church creation evolution faith family game genesis God gog good google government heart iracing jesus life linux lord love matthew music pc poem poetry quote racing romantical salvation science sim sin steam truth update video windows word YouTube

Log in
© 2026 Selah. | Powered by Superbs Personal Blog theme