How to round a double to 2 decimal places in Java

1 Answer

0 votes
import java.text.DecimalFormat;

public class MyClass {
    public static void main(String args[]) {
        double pi = 3.14159265359;
        
        DecimalFormat df = new DecimalFormat("#.##");
        
        double rounded = Double.parseDouble(df.format(pi));
        
        System.out.println(rounded);
    }
}





/*
run:
     
3.14

*/

 



answered Nov 24, 2023 by avibootz

Related questions

2 answers 257 views
3 answers 195 views
1 answer 164 views
3 answers 285 views
1 answer 111 views
1 answer 146 views
1 answer 158 views
...