No, there is no built-in function that I know of.
There is an easy way to do this. Double.toString will give you a string containing ALL significant decimal digits in double. Some properties of this line are listed below:
- A string that can be represented in decimal notation or scientific notation, depending on the value of double.
- Doubles that convert to decimal numbers 10,000,000 or more or less than 1/1000 lead to scientific notation. Otherwise, they are in decimal notation.
Using Double.toString to determine how many decimal places essentially consists of how many significant digits to the right of the decimal point minus the scientific notation indicator, if any. The decimal notation will always have at least one digit to the right of the decimal point and at least one digit to the left of the decimal point, even if it is zero. Since we are concerned about decimals for significant digits, the trailing zero to the right of the decimal point is a problem and should not be considered a decimal point.
The following code will do a good calculation for you:
StringBuffer stringBuffer = new StringBuffer(Double.toString(1234.567890D)); System.out.println(stringBuffer.toString()); int i; // general purpose character index int exponent; int decimalPlaces; if ((i = stringBuffer.indexOf("E")) > -1) { // scientific notation... // turn scientific notation exponent into an integer exponent = Integer.parseInt(stringBuffer.substring(i + 1)); // truncate the exponent from the StringBuffer stringBuffer = stringBuffer.delete(i, stringBuffer.length()); } else { // decimal notation, could be trailing zero exponent = 0; // no exponent, so zero // point i to trailing zero and truncate it, if there is one if (stringBuffer.charAt((i = stringBuffer.length() - 1)) == '0') { stringBuffer = stringBuffer.deleteCharAt(i); // delete trailing zero } } // stringBuffer now contains only significant digits to the // right of the decimal point, if there are any decimalPlaces = stringBuffer.length() - 1 - stringBuffer.indexOf(".") - exponent; // zero or positive number is decimal places // negative number is number of zeroes to the left of the decimal point // between the decimal point and the least significant digit System.out.println(decimalPlaces);
I have some questions about this question. What accuracy is expected with a decimal representation of a double? Are doubles used to perform decimal calculations improperly? Decimal calculations with decimal fractions using floats and doubles can have results that unexpectedly have 16 or 17 significant digits and can only be approximations of the expected results from equivalent decimal calculations.
One aspect of the float, doubles, doubles (aka quads), which seems to be programmers and designers that all of these formats are actually stored as binary fractional numbers that can only approximate decimal numbers, except for a very, very small number, most of which are pretty are close to the values 1, -1, plus the value 0. As you move to positive infinity or zero from 1 or to negative infinity or zero from -1, the approximation sparseness becomes apparent.
Almost all decimal fractions do not have a direct representation in floats and doubles. Only decimal fractions, which may consist of the sum of some combination of the following series of fractions, have an accurate representation:
1, 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128, 1/256, ..., 1/4503599627370496
All others are approximations.
Integers greater than +9007199254740992 or less than -9007199254740992 may not have an accurate representation, and sparseness increases exponentially, since integers increase above positive or decrease below negative values, respectively.
Another way to look at this is to understand that 64-bit IEEE doubles, normalizes, approximates positive and negative decimal numbers, which have absolute values ranging from 2.225073858507201400 E -308 to 1.797693134862315700 E +308. However, only 1,8446744073709551616 E +19 values are available for these approximations. This means that decimal values in the E + 607 format share the view with some other decimal values that come closer to double.
The behavior of floats and doubles destroys chaos with decimal calculations requiring exact decimal precision, such as financial calculations, and therefore, if high-precision approximation is acceptable, scaled integers and long numbers or classes such as BigInteger and BigDecimal should be used for calculations requiring exact decimal precision, rounding and precision.