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 222 views
1 answer 248 views
1 answer 216 views
1 answer 248 views
1 answer 247 views
1 answer 266 views
...