Format double to 2 decimal places java: Easy Steps to implement it

Java is a strong, contemporary, object-oriented, high-level, secure programming language. Sun Microsystems was the company that first created the Java programming language. It was also published in 1995. So we will give you an efficient implementation of Format double to 2 decimal places java. In the event that you were unaware of such knowledge prior to today. So let’s attentively study this article. so we’ll get started straight away.

As a result, in this piece, we’ll look at how to format double to two decimal places. There are several methods for formatting double to two decimal places. So let’s examine each of them individually now.Format double to 2 decimal places java

DecimalFormat(“0.00”)

In order to ensure that the number is rounded to two decimal places, use DecimalFormat("0.00")


package com.mkyong.math.rounding;

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

public class DecimalExample {

    private static final DecimalFormat df = new DecimalFormat("0.00");

    public static void main(String[] args) {

        double input = 3.14159265359;

        System.out.println("double : " + input);
        System.out.println("double : " + df.format(input));    //3.14

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

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

    }

}

Output


double : 3.14159265359
double : 3.14

double (RoundingMode.DOWN) : 3.14
double (RoundingMode.UP)  : 3.15

String.format(“%.2f”)

The double can alternatively be rounded to two decimal places using String formater %2f. However, we are unable to change the rounding mode in String.format, which always rounds half-up.

package com.mkyong.math.rounding;

public class StringFormatExample {

  public static void main(String[] args) {

      double input = 3.14159265359;
      System.out.println("double : " + input);
      System.out.println("double : " + String.format("%.2f", input));
      System.out.format("double : %.2f", input);

  }

}

Output


double : 3.14159265359
double : 3.14
double : 3.14

BigDecimal

package com.mkyong.math.rounding;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalExample {

  public static void main(String[] args) {

      double input = 3.14159265359;
      System.out.println("double : " + input);

      BigDecimal bd = new BigDecimal(input).setScale(2, RoundingMode.HALF_UP);
      double newInput = bd.doubleValue();

      System.out.println("double : " + newInput);

  }
}

Output


double : 3.14159265359
double : 3.14

NumberFormat

Additionally, you may limit a number’s decimal places using the NumberFormat‘s setMaximumFractionDigits() function and format double to two decimal places using the NumberFormat’s format() method.
Here’s an illustration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.arpit.java2blog;
import java.text.NumberFormat;
import java.util.Formatter;
public class NumberFormatformatDouble {
    public static void main(String[] args) {
        double d1 = 2.009;
        double d2 = 2.979;
        NumberFormat nf= NumberFormat.getInstance();
        nf.setMaximumFractionDigits(2);
        System.out.println(“Double d1 upto 2 decimal places: “ +nf.format(d1));
        System.out.println(“Double d2 upto 2 decimal places: “ +nf.format(d2));
    }
}

Output:

Double d1 upto 2 decimal places: 2.01
Double d2 upto 2 decimal places: 2.98

Formatter

The format() function of the java.util.Formatter‘s class may be used to format a double value to two decimal places. This technique is comparable to System.out.printf.
As an illustration:

Output:

Double upto 2 decimal places: 2.46

Conclusion

Above is all the useful information about Format double to 2 decimal places java that we want to bring to you. We hope you will have interesting experiences about them. If you have any problems with this experience, you can contact Onextdigital, which is always ready to help you find a solution.

Additionally, if you’re looking for economic development for your eCommerce business, go no further than Onextdigital. We also offer cost-effective Shopify Development, Bigcommerce Development, and Magento Development services in addition to a group of knowledgeable specialists and developers who collaborate directly with the designers.