A summary of several ways Java retains two decimal places _java

Source: Internet
Author: User

Several methods are listed in this article:

1. Usejava.math.BigDecimal

2. Usejava.text.DecimalFormat

3. Usejava.text.NumberFormat

4. Usejava.util.Formatter

5. UseString.format

The end of the article to share more expansion of knowledge, but also to achieve their own or borrow encapsulated good class library to achieve, in this article will not be enumerated. Let's take a look at the detailed introduction.

First, use BigDecimal, retain two decimal places

public static String FORMAT1 (double value) {

 BigDecimal bd = new BigDecimal (value);
 BD = Bd.setscale (2, roundingmode.half_up);
 return bd.tostring ();
}

Second, use DecimalFormat, retain two decimal places

public static String FORMAT2 (double value) {

 DecimalFormat df = new DecimalFormat ("0.00");
 Df.setroundingmode (ROUNDINGMODE.HALF_UP);
 return Df.format (value);
}

Third, use NumberFormat, retain two decimal places

public static String FORMAT3 (double value) {

 NumberFormat nf = numberformat.getnumberinstance ();
 Nf.setmaximumfractiondigits (2);
 * *
  Setminimumfractiondigits set to 2 * * * * * 
  if not, then return
  * Instead of 100.00 when value is 100.00
  * *
 nf.setminimumfractiondigits (2);
 Nf.setroundingmode (ROUNDINGMODE.HALF_UP);
  * * * If the format to be exported is separated by commas, it can be set to True
 /nf.setgroupingused (false);
 return Nf.format (value);
}

Four, use java.util.Formatter, keep two digits after the decimal point

The public static String FORMAT4 (double value) {/
  *%.2f% indicates that the result of an arbitrary number of digits before the decimal point 2 represents a two-bit decimal format is F for floating-point type */return
 NE W Formatter (). Format ("%.2f", Value). toString ();

V. Use String.Format to achieve.

public static String FORMAT5 (double value) {return

 String.Format ("%.2f", Value). toString ();

Extending knowledge

String.format As a text processing tool, we provide powerful and rich string formatting capabilities.

To format floating point numbers                      

Placeholder format is:%[index$][logo]*[minimum width] [. precision] Converter

Double num = 123.4567899;
System.out.print (String.Format ("%f%n", num)); 123.456790 
System.out.print (String.Format ("%a%n", num));//0X1.EDD3C0BB46929P6 
System.out.print ( String.Format ("%g%n", num)); 123.457

Available Identities:

--left-aligned within the minimum width, and cannot be used with 0 identities.

0, if the content length is less than the minimum width, then on the left with 0来 padding.

#, add 0x before adding a 0,16 to the 8 and 16, 8 before the system.

+, the result always contains a + or-number.

Spaces, plus spaces before positive numbers, negative numbers before adding-number.

, and used only with decimal, every 3 digits, separated.

(If the result is negative, enclose in parentheses without displaying the symbol.)

Available conversion characters:

B, a Boolean type that is formatted as String true or False if the argument is a Boolean type that is not false.

N, a platform-independent newline character can also be obtained by System.getproperty ("Line.separator").

F, floating-point type (decimal). Displays a 9-digit valid number and is rounded. such as 99.99.

A, floating-point type (hexadecimal).

E, exponential type. such as 9.38e+5.

g, floating-point type (shorter than%f,%a length, displaying 6 digits, and rounding)

Summarize

The above is to keep two decimal places in Java to write all the content, I hope the content of this article for everyone's study or work can bring some help, if you have questions you can message exchange.

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.