Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The precision loss issue of new BigDecimal(double/float) #91

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading