[CSC 204] Rounding to Three Decimal Places

Andrew J. Pounds pounds at theochem.mercer.edu
Thu Jan 27 18:44:18 EST 2011


In your current project you are asked to "round" the results to three 
decimal places.  While we will talk about numeric formatting later in 
the semester, there is a much simpler way to solve the problem of 
rounding associated with your current project.

You need to make sure you import java.lang.Math at the top of your 
code.   Then you can do something like this

If, for example, I want to round a value to three decimal points, I can 
do the following.

roundedResult = Math.round(1000.0 * value) / 1000.0;

if, on the other hand, I wanted to round it to 2 decimal places...

roundedResult = Math.round(100.0 * value) / 100.0;


Let me know if you have questions about this.


-- 
Andrew J. Pounds, Ph.D.  (pounds at theochem.mercer.edu)
Associate Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627



More information about the csc204 mailing list