public class SimpleDecimaliser extends Object implements Decimaliser
Decimaliser
, which employs a simple rounding technique for converting floating-point
numbers to decimal representation. The range for conversion is between 1e-18 and Long.MAX_VALUE
.
This implementation is designed for performance and simplicity, at the cost of potentially reduced accuracy. It is useful in scenarios where performance is a higher priority than precision.
Modifier and Type | Field and Description |
---|---|
static int |
LARGEST_EXPONENT_IN_LONG
The largest exponent that can be represented using a long integer.
|
static Decimaliser |
SIMPLE
A singleton instance of
SimpleDecimaliser for convenient reuse. |
Constructor and Description |
---|
SimpleDecimaliser() |
Modifier and Type | Method and Description |
---|---|
boolean |
toDecimal(double value,
DecimalAppender decimalAppender)
Converts a double value to its decimal representation using a simple rounding approach, and appends it to the provided
DecimalAppender . |
boolean |
toDecimal(float value,
DecimalAppender decimalAppender)
Converts a float value to its decimal representation using a simple rounding approach, and appends it to the provided
DecimalAppender . |
public static final int LARGEST_EXPONENT_IN_LONG
public static final Decimaliser SIMPLE
SimpleDecimaliser
for convenient reuse.
This instance is thread-safe and can be used across multiple threads without synchronization.public boolean toDecimal(double value, DecimalAppender decimalAppender)
DecimalAppender
.
This method iteratively scales the input value by powers of 10, and performs rounding to attempt finding a precise
representation. If such representation is found, it is appended using the provided DecimalAppender
.
toDecimal
in interface Decimaliser
value
- The double value to be converted.decimalAppender
- The DecimalAppender
used to store and append the converted decimal value.true
if the conversion and appending were successful, false
otherwise.public boolean toDecimal(float value, DecimalAppender decimalAppender)
DecimalAppender
.
This method iteratively scales the input value by powers of 10, and performs rounding to attempt finding a precise
representation. If such representation is found, it is appended using the provided DecimalAppender
.
toDecimal
in interface Decimaliser
value
- The float value to be converted.decimalAppender
- The DecimalAppender
used to store and append the converted decimal value.true
if the conversion and appending were successful, false
otherwise.Copyright © 2023. All rights reserved.