0
Q:

java format double no decimal places

printf("%. 2f", value);
1
String result = String.format("%.2f", value);
3
package com.mkyong;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalExample {

    private static DecimalFormat df2 = new DecimalFormat("#.##");

    public static void main(String[] args) {

        double input = 3.14159265359;
        System.out.println("double : " + input);
        System.out.println("double : " + df2.format(input));    //3.14

        // DecimalFormat, default is RoundingMode.HALF_EVEN
        df2.setRoundingMode(RoundingMode.DOWN);
        System.out.println("\ndouble : " + df2.format(input));  //3.14

        df2.setRoundingMode(RoundingMode.UP);
        System.out.println("double : " + df2.format(input));    //3.15

    }

}
Copy
2
double input = 3.14159265359;
System.out.format("double : %.2f", input); // 3.14
System.out.println("double : " + String.format("%.2f", input)); // 3.14
1
DecimalFormat df = new DecimalFormat("#.00"); 
0
int x = (int) y
0

New to Communities?

Join the community