toString
Returns the canonical decimal string representation of this decimal.
The resulting string is always canonical: no leading plus sign, no leading zeros before the decimal point, and no trailing zeros in the fractional part.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
fun checkToString(input: String, expected: String) {
val result: String = Decimal.parse(input).toString()
check(result == expected)
}
// Zero is always "0":
checkToString(input = "0", expected = "0")
checkToString(input = "+0", expected = "0")
checkToString(input = "-0", expected = "0")
checkToString(input = "0.0", expected = "0")
// Plus sign removed, leading zeros removed:
checkToString(input = "+42", expected = "42")
checkToString(input = "00042", expected = "42")
// Trailing fractional zeros removed:
checkToString(input = "3.10", expected = "3.1")
checkToString(input = "5.00", expected = "5")
// Canonical representations preserved:
checkToString(input = "3.14", expected = "3.14")
checkToString(input = "-2.5", expected = "-2.5")
checkToString(input = "0.001", expected = "0.001")Content copied to clipboard
Calling from Java
Here's an example of calling this function from Java code:
final BiConsumer<String, String> checkToString = (input, expected) -> {
final boolean check = String.valueOf(Decimal.parse(input))
.equals(expected);
if (!check) throw new IllegalStateException("Check failed.");
};
// Zero is always "0":
checkToString.accept("0", "0");
checkToString.accept("+0", "0");
checkToString.accept("-0", "0");
checkToString.accept("0.0", "0");
// Plus sign removed, leading zeros removed:
checkToString.accept("+42", "42");
checkToString.accept("00042", "42");
// Trailing fractional zeros removed:
checkToString.accept("3.10", "3.1");
checkToString.accept("5.00", "5");
// Canonical representations preserved:
checkToString.accept("3.14", "3.14");
checkToString.accept("-2.5", "-2.5");
checkToString.accept("0.001", "0.001");Content copied to clipboard