Java rounding Reserved Decimal method Rollup __java

Source: Internet
Author: User

In the Android calculation or display is often used in the Java rounding and retention of 1 or more decimal places, before only using BigDecimal (can guarantee numerical accuracy, but also available to retain the number of digits), independent choice, the method is very good, transferred from http://www.cnblogs.com/ Hoojo/p/java_round_format_bigdecimal_decimalformat.html

Mode one:
double f = 3.1516;
BigDecimal B = new BigDecimal (f);
Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();
 
Mode two:
new Java.text.DecimalFormat ("#.00"). Format (3.1415926);
#.00 represents a two-bit decimal #.0000 four-bit decimal
 
, and so on ... Mode three:
double d = 3.1415926;
String result = String.Format ("%.2f", d);
%.2f%. Represents a floating-point type with a result of an arbitrary number of digits before the decimal point of 2 representing a two-bit decimal format.
 
//Method four:
Math.Round (5.2644555 *) * 0.01D;
String.Format ("%0" + + "D", 23) 23 less than 15 is on the front complement 0


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.