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 0b0e772 commit af0e628
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 @@ -754,7 +754,7 @@ public static BigDecimal fromNano(long nanoCoins, Integer precision) {

public static BigInteger toNano(long toncoins) {
checkToncoinsOverflow(BigInteger.valueOf(toncoins).multiply(BI_BLN1));
return BigInteger.valueOf(toncoins * BLN1);
return BigInteger.valueOf(toncoins).multiply(BI_BLN1);
}

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

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

public static BigInteger toNano(BigDecimal toncoins) {
Expand Down

0 comments on commit af0e628

Please sign in to comment.