public class GeneralDecimaliser extends Object implements Decimaliser
Decimaliser which employs a hybrid approach to convert floating-point numbers
to decimal representation. Initially, it attempts a lightweight conversion strategy. If that fails or is unsuitable
due to the number's magnitude, it then falls back on a BigDecimal-based strategy for higher precision.
This implementation is designed to achieve a balance between performance and precision. It is particularly well-suited for converting numbers that are usually within a certain range, but occasionally can be very large or have a high degree of precision.
Note: Numbers are represented in scientific notation if they are 1e45 or larger, or 1e-29 or smaller, to maintain compactness.
| Modifier and Type | Field and Description |
|---|---|
static Decimaliser |
GENERAL
A singleton instance of
GeneralDecimaliser for convenient reuse. |
| Constructor and Description |
|---|
GeneralDecimaliser() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
toDecimal(double value,
DecimalAppender decimalAppender)
Converts a double value to its decimal representation by first trying a lightweight approach and then,
if necessary, falling back to a
BigDecimal-based approach for higher precision. |
boolean |
toDecimal(float value,
DecimalAppender decimalAppender)
Converts a float value to its decimal representation by first trying a lightweight approach and then,
if necessary, falling back to a
BigDecimal-based approach for higher precision. |
public static final Decimaliser GENERAL
GeneralDecimaliser for convenient reuse.
This instance combines both lightweight and BigDecimal-based conversion strategies.
It is thread-safe and can be used across multiple threads without synchronization.public boolean toDecimal(double value,
DecimalAppender decimalAppender)
BigDecimal-based approach for higher precision. Appends the result
to the provided DecimalAppender.
The conversion is attempted if the absolute value is 0, or if it's in the range of 1e-29 to 1e45 (both inclusive).
toDecimal in interface Decimaliservalue - The double value to be converted.decimalAppender - The DecimalAppender to which the converted decimal value is appended.true if the conversion and appending were successful, false otherwise.public boolean toDecimal(float value,
DecimalAppender decimalAppender)
BigDecimal-based approach for higher precision. Appends the result
to the provided DecimalAppender.
The conversion is attempted if the absolute value is 0, or if it's equal to or larger than 1e-29f.
toDecimal in interface Decimaliservalue - The float value to be converted.decimalAppender - The DecimalAppender to which the converted decimal value is appended.true if the conversion and appending were successful, false otherwise.Copyright © 2023. All rights reserved.