Rounding Decimals

M

morc

hey,
I've been googling ways to round up decimals to the nearest tenth. e.g
0.266 would round up to 0.3

anybody know which class/methods i should use for this.
I can' seem to be able to locate it in Java Docs.

All suggestinos are appreciated
thanks again
-morc
 
R

Rhino

morc said:
hey,
I've been googling ways to round up decimals to the nearest tenth. e.g
0.266 would round up to 0.3

anybody know which class/methods i should use for this.
I can' seem to be able to locate it in Java Docs.

All suggestinos are appreciated
thanks again

A few years ago, I had a project where I had to round a lot of decimal
numbers to a certain number of decimal places and I asked a similar
question. I was told that you should multiply, then round, then divide to
get the desired precision.

For example, to round to the nearest tenth, multiply your number by 10, use
the integer round() method, then divide by 10 again. To round to the nearest
hundredth, multiply by 100, use the integer round() method, then divide by
100 again.

These two fragmenst illustrates the technique and includes printlns to show
each intermediate result.

double myDouble = 2.456;

System.out.println("myDouble = " + myDouble);

double myDoubleMultiplied = myDouble * 10;

System.out.println("myDoubleMultipled = " + myDoubleMultiplied);

double myDoubleRounded = Math.round(myDoubleMultiplied);

System.out.println("myDoubleRounded = " + myDoubleRounded);

double myDoubleDivided = myDoubleRounded / 10.0;

System.out.println("myDoubleDivided = " + myDoubleDivided);





double myDouble = 2.456;

System.out.println("myDouble = " + myDouble);

double myDoubleMultiplied = myDouble * 100;

System.out.println("myDoubleMultipled = " + myDoubleMultiplied);

double myDoubleRounded = Math.round(myDoubleMultiplied);

System.out.println("myDoubleRounded = " + myDoubleRounded);

double myDoubleDivided = myDoubleRounded / 100.0;

System.out.println("myDoubleDivided = " + myDoubleDivided);
 
G

Googmeister

morc said:
hey,
I've been googling ways to round up decimals to the nearest tenth. e.g
0.266 would round up to 0.3

anybody know which class/methods i should use for this.
I can' seem to be able to locate it in Java Docs.

All suggestinos are appreciated

Do you want to round for display purposes or do you
want to store the decimal number 0.3?

If the former, you can use System.out.printf() or
java.text.DecimalForamt. If the latter, you can use
java.math.BigDecimal (but you won't be able to use
double since 0.3 is not representable using IEEE
double-precision).
 
E

EJP

Rhino said:
A few years ago, I had a project where I had to round a lot of decimal
numbers to a certain number of decimal places and I asked a similar
question. I was told that you should multiply, then round, then divide to
get the desired precision.

For example, to round to the nearest tenth, multiply your number by 10, use
the integer round() method, then divide by 10 again. To round to the nearest
hundredth, multiply by 100, use the integer round() method, then divide by
100 again.

These two fragmenst illustrates the technique and includes printlns to show
each intermediate result.

double myDouble = 2.456;

System.out.println("myDouble = " + myDouble);

double myDoubleMultiplied = myDouble * 10;

System.out.println("myDoubleMultipled = " + myDoubleMultiplied);

double myDoubleRounded = Math.round(myDoubleMultiplied);

System.out.println("myDoubleRounded = " + myDoubleRounded);

double myDoubleDivided = myDoubleRounded / 10.0;

System.out.println("myDoubleDivided = " + myDoubleDivided);

This technique is used in integer or fixed-point arithmetic. If whoeever
told you this thought it would make a difference in the case of
*floating-point* arithmetic he or she was hallicunating. It neither
increases accuracy nor rounds to the desired number of decimal places.

The OP should investigate either java.txt.DecimalFormat or
java.math.BigDecimal with the scale and rounding options.
 
Joined
Nov 10, 2008
Messages
1
Reaction score
0
Hey guys,

My two friends and I just wanted to say Thank You to Rhino

We are in a Java class, and doing this math game thing where we had to choose a level (addition, multpiplication, subraction, division) and the user had to play the game

And we got to Division and couldnt figure out how to round to the nearest tenth. Cuz were stupid :p
And we googled and found this site. So thanks alot for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top