Re-learning Java (2): Processing of Java numbers

Source: Internet
Author: User
Tags rounds

A simple example

1      Public void Testnum () {2         System.out.println (Math.Round (12.5)); 3         System.out.println (Math.Round ( -12.5)); 4     }56     output:137             -12
The output is determined by theThe rounding rules of the math.round affect.

TwoRounding method in Java

1.round_up: Rounding away from 0 directions. Rounding in the direction of the absolute maximum, as long as the bit is discarded0 is rounding.

2.round_down: tends to round in 0 direction. Enter in the direction of the smallest absolute value, all the bits are discarded, there is no rounding.

3.round_ceiling: Rounds to positive infinity. To the positive maximum direction. If positive, the rounding behavior is similar toROUND_UP, if negative, rounding behavior
Similar toRound_down.The Math.Round () method is the use of this pattern.

4.round_floor: Rounds to negative infinity. To the negative Infinity direction. If positive, the rounding behavior is similar toRound_down; If negative, rounding behavior
Similar toround_up.

5.HALF_UP: Recent number rounding (5 in ). This is our most classic rounding.

6.half_down: Recent number rounding (5 Span style= "font-family: ' Consolas ';" >). Here 5 is to be discarded.

7.hail_even: Banker Rounding Method. The value of the shed bit is less than 5 when directly shed, greater than 5 o'clock carry back, equals 5 when there are other non-0 value is rounded up
, if 5 is followed by Span style= "font-family: ' Consolas ';" >0 is based on 5 in front of the number of parity judgments, odd-numbered, even-numbered homes.

Banker Algorithm Example:
1  Public Static voidMain (string[] args) {2BigDecimal d =NewBigDecimal (100000);//Deposit3BigDecimal r =NewBigDecimal (0.001865*3);//Interest4BigDecimal i = d.multiply (R). Setscale (2,roundingmode.half_even);//using the Banker algorithm5 6SYSTEM.OUT.PRINTLN ("Quarterly interest is:" +i);7                 }8 Output:9Quarterly interest is: 559.50
< Span style= "font-family: ' Consolas ';" >< Span style= "font-family: ' Consolas ';" > 
Span style= "font-family: ' Consolas ';" >
Three, set precision

BigDecimal
1  Public void Testnumberscale () {2             double d = 123456.7850; 3             New BigDecimal (d); 4             double D1 = Bigdecimal.setscale (2, bigdecimal.round_half_up). Doublevalue (); 5             System.out.println (D1); 6         }
< Span style= "font-family: ' Consolas ';" >< Span style= "font-family: ' Consolas ';" > here use bigdecimal use setscale set precision, use  ROUND_HALF_UP set rounding rules. 

decimalformat
Span style= "font-family: ' Consolas ';" >
 1  public  void   TestNumberScale2 () { 2  double  d = 123456.7850;  3  decimalformat decimalformat = new  DecimalFormat ("##.00"  4  String d1 = Decimalformat.format (d); 5   System.out.println (D1);  6 } 
< Span style= "font-family: ' Consolas ';" >< Span style= "font-family: ' Consolas ';" >< Span style= "font-family: ' Consolas ';" > 
string.format

 1  public  void   TestNumberScale3 () { 2  double  d = 123456.7850;  3  String d1 = String.Format ("%.2f" ,d); 4   System.out.println (D1);  5 } 
< Span style= "font-family: ' Consolas ';" >< Span style= "font-family: ' Consolas ';" > 
< Br> note decimalformat rounding rules and 3 different

Re-learn Java (2): Handling of Java numbers

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.