How can you get decimal places to be rounded up? Like for instance if you had 3.125 and you wanted it to be output as 3.13.
Printable View
How can you get decimal places to be rounded up? Like for instance if you had 3.125 and you wanted it to be output as 3.13.
public double roundMe(int positionRoundingTo, double original)
// I could do this for you but I would feel guilty if it was your homework.
// Therefore I'll do it by example:
// you seem to be wanting to go to two decimal places
roundMe(2, 3.125);
double multiplier = 10.0 ^ (positionRoundingTo)
ie 100.0 = 10 ^ 2;
(since you want two decimal points)
3.125 * multiplier = 312.5
(3.125 * 100.0 = 312.5)
The trick with rounding is to add (0.5) once you have the decimal in the right spot.
312.5 + 0.5 = 313.0
then you keep just the integer portion of 313.0, resulting in 313.
you then make 313 back into a double.
313.0
then you divide it by that multiplier number determined above.
313.0/100.0 = 3.13
and return it as your answer:
3.13