SUMMARY:
double roundit(double num, double N)
{
double d = log10(num);
double power;
if (num > 0)
{
d = ceil(d);
power = -(d-N);
}
else
{
d = floor(d);
power = -(d-N);
}
return (int)(num * pow(10.0, power) + 0.5) * pow(10.0, -power);
}
So you need to find the decimal place of the first non-zero digit, then save the next N-1 digits, then round the Nth digit based on the rest.
We can use log to do the first.
log 1239451 = 6.09
log 12.1257 = 1.08
log 0.0681 = -1.16
So for numbers > 0, take the ceil of the log. For numbers < 0, take the floor of the log.
Now we have the digit d
: 7 in the first case, 2 in the 2nd, -2 in the 3rd.
We have to round the (d-N)
th digit. Something like:
double roundedrest = num * pow(10, -(d-N));
pow(1239451, -4) = 123.9451
pow(12.1257, 1) = 121.257
pow(0.0681, 4) = 681
Then do the standard rounding thing:
roundedrest = (int)(roundedrest + 0.5);
And undo the pow.
roundednum = pow(roundedrest, -(power))
Where power is the power calculated above.
About accuracy: Pyrolistical's answer is indeed closer to the real result. But note that you can't represent 12.1 exactly in any case. If you print the answers as follows:
System.out.println(new BigDecimal(n));
The answers are:
Pyro's: 12.0999999999999996447286321199499070644378662109375
Mine: 12.10000000000000142108547152020037174224853515625
Printing 12.1 directly: 12.0999999999999996447286321199499070644378662109375
So, use Pyro's answer!