BigDecimal the best way to preserve the decimal point in Java __java

Source: Internet
Author: User

[Original]bigdecimal Java, the best way to keep the decimal point

2014-3-31 Reading 332 Comments 0 Java, there are four ways to keep the decimal places, as follows:

<span style= "padding:0px; margin:0px; font-family: ' Microsoft Yahei '; font-size:14px; "
>import Java.math.BigDecimal;
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
        public class Format {Double f = 231.52;//0.9,2 public void M1 () {BigDecimal bg = new BigDecimal (f);
        Double f1 = Bg.setscale (2, bigdecimal.round_half_up). Doublevalue ();
    System.out.println (F1);
        /** * DecimalFormat Conversion is easiest/public void m2 () {DecimalFormat df = new DecimalFormat ("#.00");
    System.out.println (Df.format (f));
    /** * String.Format Print easiest/public void m3 () {System.out.println (String.Format ("%.2f", f));
        public void M4 () {NumberFormat NF = numberformat.getnumberinstance ();
        Nf.setmaximumfractiondigits (2);
    System.out.println (Nf.format (f));
        public static void Main (string[] args) {format f = new format ();
        F.M1 ();
       F.M2 (); F.M3 ();
    F.M4 (); }}</span>

But as on, for integers and decimal fraction m2,m3,m4 These three methods have a problem, so it is best to use the M1 BigDecimal, introduced as follows:

The Bigdecimal.setscale () method is used to format the decimal point
Setscale (1) means to keep a decimal number by default by rounding
Setscale (1,bigdecimal.round_down) directly remove the extra decimal places, such as 2.35 will become 2.3
Setscale (1,BIGDECIMAL.ROUND_UP) carry handle, 2.35 becomes 2.4
Setscale (1,bigdecimal.round_half_up) rounded, 2.35 turned 2.4

Setscaler (1,bigdecimal.round_half_down) rounded, 2.35 becomes 2.3, if 5 is down-shed

Setscaler (1,bigdecimal.round_ceiling) close to rounding of positive infinity

Setscaler (1,bigdecimal.round_floor) is close to the rounding of the negative infinity, and the digital >0 is the same as the round_up, the number <0 and the Round_down function

Setscaler (1,bigdecimal.round_half_even) is rounded to the nearest number, and if the distance from two adjacent digits is equal, it is rounded to the adjacent even number.
Note: 1:scale refers to the number of digits after your decimal point. For example, 123.456 score is 3 score () is the method in the BigDecimal class.
For example: BigDecimal B = new BigDecimal ("123.456"); B.scale (), the return is 3.
2:roundingmode is a retention pattern for decimals. They are all constant fields in BigDecimal, and there are many kinds.
For example: BIGDECIMAL.ROUND_HALF_UP says is 4 homes 5.
3:pubilc BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
I use a BigDecimal object divided by the result of divisor, and I want this result to keep a scale decimal place, roundingmode means what the retention mode is, rounded or otherwise, you can choose for yourself.

4: For the general Add, subtract, multiply method of decimal format is as follows: BigDecimal mdata = new BigDecimal ("9.655"). Setscale (2, Bigdecimal.round_half _UP);
System.out.println ("mdata=" + mdata); ----Results:-----mdata=9.66

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.