Skip to content

Commit

Permalink
Merge pull request #91 from qzmer1104/feat-fix-utils
Browse files Browse the repository at this point in the history
fix: The precision loss issue of new BigDecimal(double/float)
  • Loading branch information
neodix42 authored Dec 8, 2024
2 parents 11dac5a + af0e628 commit 65cae20
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 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 @@ -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 @@ -769,20 +769,20 @@ 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");
}
return BigDecimal.valueOf(toncoins * BLN1).toBigInteger();
return BigDecimal.valueOf(toncoins).multiply(BigDecimal.valueOf(BLN1)).toBigInteger();
}

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");
}
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 65cae20

Please sign in to comment.