3

The following will ensure that any large numbers will only be precise to the hundredths place (related to this answer):

public function round( sc:Number ):Number
{
    sc = sc * 100;
    sc = Math.floor( sc );
    sc = sc / 100;

    return sc;
}

What is the optimal way to round my numbers to the precision of .05? Is there something clever to be done with bit-shifting to get this result? For example, I would like:

3.4566 = 3.45

3.04232 = 3.05

3.09 = 3.1

3.32 = 3.3

Community
  • 1
  • 1
jedierikb
  • 12,752
  • 22
  • 95
  • 166

2 Answers2

6

You could multiply by 20, round, then divide by 20.

Edit: You will want to use Math.round() instead of Math.floor(), otherwise your test case 3.09 will turn into 3.05.

Chris Thornhill
  • 5,321
  • 1
  • 24
  • 21
2

You could just change the *100 to *20 and /100 /20. But why stop there, when you can naturally generalize?

double floor(double in, double precision) {
    return Math.floor(in/precision)*precision
}
//floor(1.07, 0.05) = 1.05
Craig Gidney
  • 17,763
  • 5
  • 68
  • 136