How Java gets the number of decimal digits of a double

Source: Internet
Author: User

Objective

Look at the title is not think this is a very simple problem, I think so at the beginning, but the actual situation, in various cases we have tested, found that many of the actual situation can not be unsatisfactory.

Method analysis

What you can think of now is easier to have the following several
1, directly using double processing
2. Convert double to string for processing

Method One: The double is processed directly, the calculation is calculated by the result of the modulo operation to obtain the scale, as follows:

     Public Static intGetnumberdecimaldigits (DoubleNumber ) {        if(Number = = (Long) {return0; }        inti = 0;  while(true) {i++; if(Number * MATH.POW (ten, i)% 1 = = 0) {                returni; }        }    }

Method Two: After the double is converted to a string, then use the "." The segmentation is judged as follows: (here a double converts a string there are several ways we choose one of these)

 Public Static intGetnumberdecimaldigits (DoubleNumber ) {String Moneystr=string.valueof (number); string[] Num= Moneystr.split ("\ \.")); if(Num.length = = 2) {         for (;;) {            if(Num[1].endswith ("0") ) {num[1] = num[1].substring (0, Num[1].length ()-1); }Else {                 Break; }        }        returnNum[1].length (); }Else {        return0; }}

Test method:

 Public Static voidMain (string[] args) {System.out.print (Getnumberdecimaldigits (0)); System.out.print ("" + getnumberdecimaldigits (0.0)); System.out.print ("" + getnumberdecimaldigits (1)); System.out.print ("" + getnumberdecimaldigits (-11.1)); System.out.print ("" + getnumberdecimaldigits (1.01)); System.out.print ("" + getnumberdecimaldigits (0.0100)); System.out.print ("" + getnumberdecimaldigits (0.123456)); System.out.print ("" + getnumberdecimaldigits (11111111.1234)); System.out.print ("" + getnumberdecimaldigits (11.0123456789));}

Results analysis

Test results:
Method One: 0 0 0 1 2 2 6 5 11
Method Two: 0 0 0 1 2 2 6 13 10

Why is the final result different from what we imagined???
There are three reasons:
1, double calculation loss accuracy:
2.01 * 10 = 20.099999999999998

2, double to string loss of precision:
String.valueof (11111111.1234) = 1.11111111234E7
11111111.1234 + "" = 1.11111111234E7
Double.valueof (11111111.123) = 1.1111111123E7
...

Summarize

So in the current situation, there are a variety of problems when judging with double, because of the problem of double precision.
So the only solution is, do not use a double, directly using the string, whether it is the value passed from the client or the production value is directly using the string for processing and judgment, the method is similar to the method two.
If you do not want to make calculations, use BigDecimal for calculations. New BigDecimal. Note Be sure to use a string as a parameter for the new

If you have a better way, can pass the above test, then hurriedly comment let me worship worship ~

Problems raised and resolved abroad:
Https://stackoverflow.com/questions/6264576/number-of-decimal-digits-in-a-double?utm_medium=organic&utm_ Source=google_rich_qa&utm_campaign=google_rich_qa

How Java gets the number of decimal digits of a double

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.