How to convert a number to 2 decimal places in Java

1 Answer

0 votes
import java.text.DecimalFormat;

public class MyClass {
    public static void main(String args[]) {
        double d = 3.141592;
        
        DecimalFormat df = new DecimalFormat("#.00");
        
        String dformated = df.format(d);
        System.out.println(dformated); 

    }
}




/*
run:
 
3.14
 
*/

 



answered Aug 18, 2021 by avibootz

Related questions

1 answer 135 views
1 answer 153 views
1 answer 123 views
1 answer 144 views
1 answer 159 views
1 answer 167 views
...