forked from Qortal/qortal
Fix for incorrect amounts reported by API
This commit is contained in:
parent
28991a926f
commit
7102f4a727
@ -12,21 +12,7 @@ public abstract class Amounts {
|
|||||||
public static final BigInteger ROUNDING = MULTIPLIER_BI.subtract(BigInteger.ONE);
|
public static final BigInteger ROUNDING = MULTIPLIER_BI.subtract(BigInteger.ONE);
|
||||||
|
|
||||||
public static String prettyAmount(long amount) {
|
public static String prettyAmount(long amount) {
|
||||||
StringBuilder stringBuilder = new StringBuilder(20);
|
return String.format("%d.%08d", amount / MULTIPLIER, amount % MULTIPLIER);
|
||||||
|
|
||||||
stringBuilder.append(amount / MULTIPLIER);
|
|
||||||
|
|
||||||
stringBuilder.append('.');
|
|
||||||
|
|
||||||
int dpLength = stringBuilder.length();
|
|
||||||
|
|
||||||
stringBuilder.append(Math.abs(amount % MULTIPLIER));
|
|
||||||
|
|
||||||
int paddingRequired = 8 - (stringBuilder.length() - dpLength);
|
|
||||||
if (paddingRequired > 0)
|
|
||||||
stringBuilder.append("00000000", 0, paddingRequired);
|
|
||||||
|
|
||||||
return stringBuilder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BigDecimal toBigDecimal(long amount) {
|
public static BigDecimal toBigDecimal(long amount) {
|
||||||
|
22
src/test/java/org/qortal/test/AmountsTests.java
Normal file
22
src/test/java/org/qortal/test/AmountsTests.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package org.qortal.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.qortal.utils.Amounts;
|
||||||
|
|
||||||
|
public class AmountsTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrettyAmount() {
|
||||||
|
testPrettyAmount(1L, "0.00000001");
|
||||||
|
testPrettyAmount(100000L, "0.00100000");
|
||||||
|
testPrettyAmount(100000000L, "1.00000000");
|
||||||
|
testPrettyAmount(1000000000L, "10.00000000");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testPrettyAmount(long amount, String prettyAmount) {
|
||||||
|
assertEquals(prettyAmount, Amounts.prettyAmount(amount));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user