in src/main/java/org/apache/commons/text/numbers/ParsedDecimal.java [144:204]
public static ParsedDecimal from(final double d) {
if (!Double.isFinite(d)) {
throw new IllegalArgumentException("Double is not finite");
}
// Get the canonical string representation of the double value and parse
// it to extract the components of the decimal value. From the documentation
// of Double.toString() and the fact that d is finite, we are guaranteed the
// following:
// - the string will not be empty
// - it will contain exactly one decimal point character
// - all digit characters are in the ASCII range
final char[] strChars = Double.toString(d).toCharArray();
final boolean negative = strChars[0] == MINUS_CHAR;
final int digitStartIdx = negative ? 1 : 0;
final int[] digits = new int[strChars.length - digitStartIdx - 1];
boolean foundDecimalPoint = false;
int digitCount = 0;
int significantDigitCount = 0;
int decimalPos = 0;
int i;
for (i = digitStartIdx; i < strChars.length; ++i) {
final char ch = strChars[i];
if (ch == DECIMAL_SEP_CHAR) {
foundDecimalPoint = true;
decimalPos = digitCount;
} else if (ch == EXPONENT_CHAR) {
// no more mantissa digits
break;
} else if (ch != ZERO_CHAR || digitCount > 0) {
// this is either the first non-zero digit or one after it
final int val = digitValue(ch);
digits[digitCount++] = val;
if (val > 0) {
significantDigitCount = digitCount;
}
} else if (foundDecimalPoint) {
// leading zero in a fraction; adjust the decimal position
--decimalPos;
}
}
if (digitCount > 0) {
// determine the exponent
final int explicitExponent = i < strChars.length
? parseExponent(strChars, i + 1)
: 0;
final int exponent = explicitExponent + decimalPos - significantDigitCount;
return new ParsedDecimal(negative, digits, significantDigitCount, exponent);
}
// no non-zero digits, so value is zero
return new ParsedDecimal(negative, new int[] {0}, 1, 0);
}