Java retains two decimal places

Source: Internet
Author: User

Java methods for preserving decimal problems:

Method One: Rounding using Java. Math.bigdecimal class

 Double d = 12.345;

BigDecimal bd = new BigDecimal (2,bigdecimal.round_half_up). Doublevalue ();

Method Two: Use the Java.text.DecimalFormat class

  Double d = 12.345;

DecimalFormat Dformat = new DecimalFormat (". 00");

Dformat.format (d);

  (". 00") indicates that two decimal places are reserved after the decimal point

Method Three: Use the Java.lang.Math class

  Double d = 12.345;

Math.Round (d);

Math.Round (double D);//return lang type

Math.Round (float f);//return int type

Today when you use this method to get a number that retains 2 decimals, the resulting integer is always the code as follows:

STANDARDP = (double) Math.Round (STANDARD * 10000))/100 + "%";//standard the value of the double type obtained by dividing the two variables

Later, the API found that the return value type was lang, and the forced type conversion was resolutely enforced. So be careful when you use this method to preserve decimals.

Finally, remember the BigDecimal class

BigDecimal is the immutable, arbitrary-precision, signed decimal number provided by Java. The user has full control over the rounding behavior and throws an exception if no rounding mode is specified and the exact result cannot be represented; otherwise, the calculation can be performed on the selected precision and rounding mode by providing the appropriate Mathcontext object for the operation.

This class provides arithmetic, scale operations, rounding, comparisons, hashing algorithms, and format conversion operations.

BigDecimal (double D); Constructs a BigDecimal object with a double type decimal number;

BigDecimal (string s); BigDecimal object created by a string representation object;

For constructing a BigDecimal object using a double type, the Java EE describes this in the API documentation:

Note:

    1. The result of this construction method is somewhat unpredictable. One might think that the BigDecimal created by writing new BigDecimal (0.1) in Java is exactly equal to 0.1 (non-scale value 1, with a scale of 1), but it actually equals 0.100000000000000 0055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as a double(or, for that case, it cannot be represented as any finite-length binary decimal). This way, the value passed into the construction method does not exactly equal 0.1 (although it is on the surface equal to the value).
    2. On the other hand, theString construction method is fully predictable: writing new BigDecimal ("0.1") creates a BigDecimal, which is exactly equal to the expected 0.1. Therefore, in comparison, it is generally advisable to use the String construction method first.
    3. When a double must be used as a source for BigDecimal , be aware that this construction method provides an accurate conversion; it does not provide the same result as the following: First use the Double.toString(double) method, and then use the BigDecimal(String) constructor method to The c15>double is converted to String. To get the result, use the static valueOf(double) method.

Look at the following results:

System.out.println (New BigDecimal (123456789.02). toString ());

System.out.println (New BigDecimal ("1234567.8902"). toString ());

The output is:

123456789.01999999582767486572265625

1234567.8902

So if you want to calculate exactly, it's not a string.

Java retains two decimal places

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.