Comparison between double and bigdecimal

Source: Internet
Author: User

1] precise floating point operations:
In Java, sometimes accurate data is required to ensure the accuracy of values. First, an example is provided to identify the problem:

Java code
  1. Public class floatnumbertester {
  2. Public static void main (string ARGs []) {
  3. System. Out. println (0.05 + 0.01 );
  4. System. Out. println (1.0-0.42 );
  5. System. Out. println (4.015*100 );
  6. System. Out. println (123.3/100 );
  7. }
  8. }


According to our expectation, what should be the above results? But we will find the problem after looking at the output:

Java code
  1. 0.060000000000000005
  2. 0.5800000000000001
  3. 401.49999999999994
  4. 1.2329999999999999


In this case, the problem is relatively serious. If we use a transaction of 123.3 yuan, the computer rejects the transaction because of 1.2329999999999999, isn't it very different from the actual situation.

[2] Rounding:
Another computing problem is rounding out. However, Java computing itself does not support rounding, for example:

Java code
  1. Public class getthrowtester {
  2. Public static void main (string ARGs []) {
  3. System. Out. println (4.015*100.0 );
  4. }
  5. }

 
The output is:
401.49999999999994
Therefore, we will find that this situation does not guarantee rounding. If you want to rounding up, there is only one method.
Java. Text. decimalformat:

Java code
  1. Import java. Text. decimalformat;
  2. Public class numberformatmain {
  3. Public static void main (string ARGs []) {
  4. System. Out. println (New decimalformat ("0.00"). Format (4.025 ));
  5. System. Out. println (New decimalformat ("0.00"). Format (4.024 ));
  6. }
  7. }


The above code is output:

Java code
  1. 4.02
  2. 4.02


Have you found any problems? Because decimalformat uses the rounding mode, for details about the rounding mode, see the last part of this article.
[3] floating point output:
If the value of the Java floating point type is greater than 9999999.0, it is automatically converted into a scientific notation. Let's take a look at the following example:

Java code
  1. Public class floatcounter {
  2. Public static void main (string ARGs []) {
  3. System. Out. println (9969999999.04 );
  4. System. Out. println (199999999.04 );
  5. System. Out. println (1000000011.01 );
  6. System. Out. println (9999999.04 );
  7. }
  8. }


Output result:

Java code
  1. 9.96999999904e9
  2. 1.9999999904e8
  3. 1.00000001101e9
  4. 9999999.04


But sometimes we do not need scientific notation, but convert it into a string, so this may be a bit troublesome.
Summary:
Therefore, in the project, do not use basic data types such as double and long and their packaging classes for floating point and big integer operations, or use the bigdecimal provided in Java, biginteger and other big data types.
However, the differences between the two constructors of the bigdecimal class are described as follows:
New bigdecimal (string Val) and new bigdecimal (double Val)
First look at the example:

Java code
  1. Public class bigdecimalmain {
  2. Public static void main (string ARGs []) {
  3. System. Out. println (New bigdecimal (123456789.01). tostring ());
  4. System. Out. println (New bigdecimal ("123456789.01"). tostring ());
  5. }
  6. }


The output was surprising, and the difference between the two was also clear:

Java code
  1. 123456789.01000000536441802978515625
  2. 123456789.01


Therefore, if you want to use the original double type for correlation calculation and then convert it to the bigdecimal type, we recommend that you use this constructor with the parameter of the string type to prevent precision deviation. That is, new bigdecimal (string Val ).


Introduction to the bigdecimal rounding mode:
The rounding mode is in Java. Math. roundingmode:
Roundingmode. Ceiling: rounding mode to positive infinity. If the result is positive, the rounding behavior is similar to roundingmode. up; if the result is negative, the rounding behavior is similar to roundingmode. Down. Note that this round-robin mode never reduces the calculated value.
 

Enter a number Use ceiling rounding mode to round a number to a single digit
5.5 6
2.5 3
1.1 2
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5


Roundingmode. Down: rounding mode to zero. Never add 1 (that is, tail truncation) to the number before the discard part ). Note that this round-robin mode will never increase the absolute value of the calculated value.

Enter a number Use the down rounding mode to round a number to a single digit.
5.5 5
2.5 2
1.1 1
-1.0 -1
-1.6 -1
-2.5 -2
-5.5 -5


Roundingmode. Floor: rounding mode to the negative infinite direction. If the result is positive, the rounding behavior is similar to roundingmode. Down; if the result is negative, the rounding behavior is similar to roundingmode. Up. Note that this round-robin mode will never increase the calculated value.

Enter a number Rounds an input number to one in floor rounding mode.
5.5 5
2.3 2
1.6 1
1.0 1
-1.1 -2
-2.5 -3
-5.5 -6



Roundingmode. half_down: rounding mode to the nearest digit. If the distance between two adjacent digits is the same, round down. If the discarded part is greater than 0.5, the rounding behavior is the same as roundingmode. up; otherwise, the rounding behavior is the same as that of roundingmode. Down.

Enter a number Round to one bit in half_down input mode
5.5 5
2.5 2
1.6 2
1.0 1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -5


Roundingmode. half_even: rounding mode to the nearest digit. If the distance between two adjacent digits is the same, it is rounded to an even number. If the number on the left of the discard part is an odd number, the rounding behavior is the same as roundingmode. half_up; if it is an even number, the rounding behavior is the same as roundingmode. half_down. Note that this round-robin mode minimizes the number of accumulated errors in statistics when a series of computations are repeated. This rounding mode is also known as the bankers rounding method, which is mainly used in the United States. This rounding mode is similar to the Rounding policy used for float and double algorithms in Java.

Enter a number Use the half_even rounding mode to turn the input into one
5.5 6
2.5 2
1.6 2
1.1 1
-1.0 -1
-1.6 -2
-2.5 -2
-5.5 -6


Roundingmode. half_up: round to the nearest number. If the distance between the two adjacent numbers is equal, round up. If the discarded part is greater than or equal to 0.5, the rounding behavior is the same as roundingmode. up; otherwise, the rounding behavior is the same as that of roundingmode. Down. Note that this round-robin mode is usually rounded up in schools.

Enter a number Use the half_up rounding mode to round a single digit
5.5 6
2.5 3
1.6 2
1.0 1
-1.1 -1
-1.6 -2
-2.5 -3
-5.5 -6



Roundingmode. Unnecessary: Used to assert that a request operation has a rounding mode with precise results, so no rounding is required. If this round-robin mode is specified for the operation that generates the exact result, arithmeticexception is thrown.

Enter a number Use the unnecessary Mode
5.5 Throw arithmeticexception
2.5 Throw arithmeticexception
1.6 Throw arithmeticexception
1.0 1
-1.0 -1.0
-1.1 Throw arithmeticexception
-1.6 Throw arithmeticexception
-2.5 Throw arithmeticexception
-5.5 Throw arithmeticexception



Roundingmode. Up: A rounding mode that is far away from zero-direction rounding. Always add 1 to the number in front of the non-zero discard part. Note that this rounding mode never reduces the absolute value of the calculated value

Enter a number Use the up rounding mode to round the input number to a single digit.
5.5 6
1.6 2
1.1 2
1.0 1
-1.1 -2
-1.6 -2
-2.5 -3
-5.4 -6




Java code
  1. Import java. Math. bigdecimal;
  2. Import java. Text. decimalformat;
  3. /**
  4. * Use the rounding mode for formatting
  5. **/
  6. Public class doubleformat {
  7. Public static void main (string ARGs []) {
  8. Doubleformat format = new doubleformat ();
  9. System. Out. println (format. doubleoutput (12.345, 2 ));
  10. System. Out. println (format. roundnumber (12.335, 2 ));
  11. }
  12. Public String doubleoutput (Double V, integer num ){
  13. If (V = double. valueof (V). intvalue ()){
  14. Return double. valueof (V). intvalue () + "";
  15. } Else {
  16. Bigdecimal B = new bigdecimal (double. tostring (V ));
  17. Return B. setscale (Num, bigdecimal. round_half_up). tostring ();
  18. }
  19. }
  20. Public String roundnumber (Double V, int num ){
  21. String fmtstring = "0000000000000000"; // 16bit
  22. Fmtstring = num> 0? "0." + fmtstring. substring (0, num): "0 ";
  23. Decimalformat dformat = new decimalformat (fmtstring );
  24. Return dformat. Format (v );
  25. }
  26. }

 
The output of this Code is:

Java code
    1. 12.35
    2. 12.34

Comparison between double and bigdecimal

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.