Java: Rounding a Double value and preserving two decimal places in several ways

Source: Internet
Author: User

1. function

The double value in the program is accurate to two digits after the decimal point. Can be rounded or truncated directly.

For example: Enter 12345.6789, the output can be 12345.68 or it can be 12345.67. As to whether rounding is required, parameters can be used to determine (Roundingmode.up/roundingmode.down, etc.).

2. Implementing the Code
Package Com.clzhang.sample;import Java.math.bigdecimal;import Java.math.roundingmode;import  Java.text.decimalformat;import Java.text.numberformat;public class Doubletest {/** * Keep two decimal places, rounding an old-fashioned way * @param d * @return */public static double FormatDouble1 (double D) {return (double) Math.Round (d*100)/1    00; }
/** * The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and Format conversion. * @param d * @return */public static double FormatDouble2 (double D) {//old method, no longer recommended for use//BIGDECIM Al bg = new BigDecimal (d). Setscale (2, bigdecimal.round_half_up);
New method, if rounding is not required, you can use roundingmode.down BigDecimal bg = new BigDecimal (d). Setscale (2, roundingmode.up);
return Bg.doublevalue (); }/** * NumberFormat is the abstract base class for all number formats. * This class provides the interface for formatting and parsing numbers. * @param d * @return */public static String FormatDouble3 (double D) {NumberFormat NF = numberformat.ge Tnumberinstance ();
Retains two decimal places nf.setmaximumfractiondigits (2);
If rounding is not required, Roundingmode.down Nf.setroundingmode (roundingmode.up) can be used;
Return Nf.format (d); }
/** * This method is quite simple. * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. * @param d * @return */public static String FormatDouble4 (double D) {DecimalFormat df = new Decimalfor Mat ("#.00");
Return Df.format (d); }
/** * This method is quite handy if you are only using formatted values in the program and then outputting them. * Should be used in this way: System.out.println (String.Format ("%.2f", d)); * @param d * @return */public static String FormatDouble5 (double D) {return String.Format ("%.2f", D); } public static void Main (string[] args) {double d = 12345.67890; System.out.println (FormatDouble1 (d)); System.out.println (FormatDouble2 (d)); System.out.println (FormatDouble3 (d)); System.out.println (FormatDouble4 (d)); System.out.println (FormatDouble5 (d)); }}
3. Output

12345.68
12345.68
12,345.68
12345.68
12345.68

In the French environment, in addition to the first two methods to display normal, the following three methods will be the decimal point is displayed as a comma, if you do internationalization to pay attention to

Java: Rounding a Double value and preserving two decimal places in several ways

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.