mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-01-31 15:22:16 +00:00
Allow zero repetitions for CoinFormat.repeatOptionalDecimals(). Also add tests.
This commit is contained in:
parent
fc923f8473
commit
9befd32200
@ -153,7 +153,7 @@ public final class CoinFormat {
|
||||
* number of repetitions
|
||||
*/
|
||||
public CoinFormat repeatOptionalDecimals(int decimals, int repetitions) {
|
||||
checkArgument(repetitions > 0);
|
||||
checkArgument(repetitions >= 0);
|
||||
List<Integer> decimalGroups = new ArrayList<Integer>(repetitions);
|
||||
for (int i = 0; i < repetitions; i++)
|
||||
decimalGroups.add(decimals);
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.google.bitcoin.utils;
|
||||
|
||||
import static com.google.bitcoin.core.Coin.COIN;
|
||||
import static com.google.bitcoin.core.Coin.CENT;
|
||||
import static com.google.bitcoin.core.Coin.SATOSHI;
|
||||
import static com.google.bitcoin.core.Coin.ZERO;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -179,6 +180,26 @@ public class CoinFormatTest {
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void repeatOptionalDecimals() {
|
||||
assertEquals("0.00000001", formatRepeat(SATOSHI, 2, 4));
|
||||
assertEquals("0.00000010", formatRepeat(SATOSHI.multiply(10), 2, 4));
|
||||
assertEquals("0.01", formatRepeat(CENT, 2, 4));
|
||||
assertEquals("0.10", formatRepeat(CENT.multiply(10), 2, 4));
|
||||
|
||||
assertEquals("0", formatRepeat(SATOSHI, 2, 2));
|
||||
assertEquals("0", formatRepeat(SATOSHI.multiply(10), 2, 2));
|
||||
assertEquals("0.01", formatRepeat(CENT, 2, 2));
|
||||
assertEquals("0.10", formatRepeat(CENT.multiply(10), 2, 2));
|
||||
|
||||
assertEquals("0", formatRepeat(CENT, 2, 0));
|
||||
assertEquals("0", formatRepeat(CENT.multiply(10), 2, 0));
|
||||
}
|
||||
|
||||
private String formatRepeat(Coin coin, int decimals, int repetitions) {
|
||||
return new CoinFormat().minDecimals(0).repeatOptionalDecimals(decimals, repetitions).format(coin).toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse() throws Exception {
|
||||
assertEquals(Coin.COIN, CoinFormat.BTC.parse("1"));
|
||||
|
Loading…
Reference in New Issue
Block a user