Answers for "how to get double value up to 2 decimal places java"

6

java double 2 decimal

String result = String.format("%.2f", value);
Posted by: Guest on June-01-2020
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
1

Display double in decimal places java

double input = 3.14159265359;
System.out.format("double : %.2f", input); // 3.14
System.out.println("double : " + String.format("%.2f", input)); // 3.14
Posted by: Guest on May-08-2020
2

specify decimal places java

double test = 12.15;
DecimalFormat df = new DecimalFormat("#.0");
System.out.println(df.format(test)); // Console: 12.2
// # - prints a digit if provided, nothing otherwise
// . - indicates where to put the decimal seperator
// 0 - prints a digit if provided, 0 otherwise
Posted by: Guest on December-29-2020
0

convert int to double with 2 decimal places java

double angle = 20.3034;
DecimalFormat df = new DecimalFormat("#.00");
String angleFormated = df.format(angle);
System.out.println(angleFormated); //output 20.30
Posted by: Guest on February-21-2021
0

how to format a double in java to 2 decimal places

DecimalFormat df = new DecimalFormat("#.00");
Posted by: Guest on May-16-2020

Code answers related to "how to get double value up to 2 decimal places java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language