Math round java
Solution 1
You could do something like this:
Double.valueOf(new DecimalFormat("#.##").format(
centimeters))); // 2 decimal-places
If you really wanted Math.round
:
(double)Math.round(centimeters * 100) / 100 // 2 decimal-places
You can have 3 decimal places by using 1000
, 4 by using 10000
etc. I personally like the first option more.
Solution 2
In order to use the Math.round
method you need to change only one line in your code:
double inches = Math.round(centimeters / 2.54);
If you want to keep 2 decimal digits you can use this:
double inches = Math.round( (centimeters / 2.54) * 100.0 ) / 100.0;
By the way I suggest you a better way to deal with these problems without rounding.
Your problem is only about displaying, so you don't need to change the model of the data, you can just change its display. To print the numbers in the format you need, you can let all your logic code like this and print the result in the following way:
-
Add this import at the beginning of your code:
import java.text.DecimalFormat;
-
Print the output in this way:
DecimalFormat df = new DecimalFormat("#.##"); System.out.println(df.format(inches) + " Inch Is " + df.format(centimeters) + " centimeters");
The string "#.##"
is the way your number will be displayed (in this example with 2 decimal digits).
Solution 3
You can print to two decimal places using the following.
System.out.printf("%.2f inch is %.2f centimeters%n", inches, centimeters);
Alex
Updated on July 09, 2022Comments
-
Alex almost 2 years
I got project do convert from cm to inch. I did it: how can i round my number with Math.round?
import java.util.Scanner; public class Centimer_Inch { public static void main (String[] args) { // 2.54cm is 1 inch Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard System.out.println("Enter the CM:"); // Write input //double double centimeters = cm.nextDouble(); double inches = centimeters/2.54; System.out.println(inches + " Inch Is " + centimeters + " centimeters"); } }
-
enrico.bacis over 11 yearsHow many decimal digits do you want to keep?
-
harold over 11 yearsWith
Math.round
, but you know that already. So what's the problem exactly? -
Admin over 11 yearsI think you're looking for String formats. (docs.oracle.com/javase/1.5.0/docs/api/java/lang/…, java.lang.Object...))
-
-
Alex over 11 yearsI must use only Math.round. Can u please explain to me how this exercise work? * 100.0 ) / 100.0
-
enrico.bacis over 11 yearsWhy should I edit it? The answer is correct, you can use the first way now and learn the second to use it when your professors will be more open-minded :)
-
Alex over 11 yearsim sorry for my bad english. I mean that I edit my post and add Question. not realy understun how (X*100) /100.0 is working?
-
enrico.bacis over 11 yearsRound is rounding the number to the closest integer. For example Math.round(3.456) = 3. So if you want to keep two decimal digits you can do this trick: Multiply by 100.0 (3.456*100.0 = 345.6), round (Math.round(345.6) = 346), and then you can divide it again (346/100.0 = 3.46)
-
masarapmabuhay almost 5 yearsI learned that if I need to include the zero (0) in the hundredths place, e.g. 432.10, I'll need to use DecimalFormat("0.00"). This is instead of DecimalFormat("#.##"). Otherwise, the output will be 432.1, without the 0. I hope this helps you. Thank you! Peace.