Trim Double to 2 decimal places

multiply the double by 100.0 and cast this to an int then take that int and cast it to a double and divide by 100.0

    int temp = (int)(longDouble*100.0);
    double shortDouble = ((double)temp)/100.0;

public static void main(String[] args) {

        double d = 6.3546;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(d));
}

here is the simple example to format the decimal value

import java.text.*;

public class DecimalPlaces {

    public static void main(String[] args) {

        double d = 1.234567;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(d));
    }

}

Tags:

Java