Skip to content

Commit

Permalink
fix: double/float is based on the IEEE 754 floating-point standard. I…
Browse files Browse the repository at this point in the history
…t uses binary floating-point representation, and some decimal numbers cannot be represented exactly in binary, leading to precision errors.
  • Loading branch information
quzhimin committed Dec 7, 2024
1 parent 11dac5a commit 0b0e772
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions utils/src/main/java/org/ton/java/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public static String streamToString(InputStream is) {
}

public static BigInteger toNano(double toncoins, Integer precision) {
return new BigDecimal(toncoins).multiply(BigDecimal.TEN.pow(precision)).toBigInteger();
return BigDecimal.valueOf(toncoins).multiply(BigDecimal.TEN.pow(precision)).toBigInteger();
}

public static BigInteger toNano(BigDecimal toncoins, Integer precision) {
Expand Down Expand Up @@ -769,7 +769,7 @@ public static BigInteger toNano(String toncoins) {

public static BigInteger toNano(double toncoins) {
checkToncoinsOverflow(
new BigDecimal(toncoins).multiply(BigDecimal.valueOf(BLN1)).toBigInteger());
BigDecimal.valueOf(toncoins).multiply(BigDecimal.valueOf(BLN1)).toBigInteger());
if (BigDecimal.valueOf(toncoins).scale() > 9) {
throw new Error("Round the number to 9 decimals first");
}
Expand All @@ -778,7 +778,7 @@ public static BigInteger toNano(double toncoins) {

public static BigInteger toNano(float toncoins) {
checkToncoinsOverflow(
new BigDecimal(toncoins).multiply(BigDecimal.valueOf(BLN1)).toBigInteger());
BigDecimal.valueOf(toncoins).multiply(BigDecimal.valueOf(BLN1)).toBigInteger());
if (BigDecimal.valueOf(toncoins).scale() > 9) {
throw new Error("Round the number to 9 decimals first");
}
Expand Down

0 comments on commit 0b0e772

Please sign in to comment.