Answers for "double 2 decimal places"

4

how to limit double decimal places java

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
Posted by: Guest on March-27-2020
8

how to set 2 decimal places in java

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));
Posted by: Guest on August-05-2020

Code answers related to "double 2 decimal places"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language