mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 10:15:52 +00:00
Formatting and parsing of monetary values with non-arabic digits.
This commit is contained in:
parent
282c25eeb4
commit
9e91c54fdd
@ -62,6 +62,7 @@ public final class CoinFormat {
|
|||||||
|
|
||||||
private final char negativeSign;
|
private final char negativeSign;
|
||||||
private final char positiveSign;
|
private final char positiveSign;
|
||||||
|
private final char zeroDigit;
|
||||||
private final char decimalMark;
|
private final char decimalMark;
|
||||||
private final int minDecimals;
|
private final int minDecimals;
|
||||||
private final List<Integer> decimalGroups;
|
private final List<Integer> decimalGroups;
|
||||||
@ -82,8 +83,8 @@ public final class CoinFormat {
|
|||||||
if (negativeSign == this.negativeSign)
|
if (negativeSign == this.negativeSign)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,8 +96,19 @@ public final class CoinFormat {
|
|||||||
if (positiveSign == this.positiveSign)
|
if (positiveSign == this.positiveSign)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set character range to use for representing digits. It starts with the specified character representing zero.
|
||||||
|
*/
|
||||||
|
public CoinFormat digits(char zeroDigit) {
|
||||||
|
if (zeroDigit == this.zeroDigit)
|
||||||
|
return this;
|
||||||
|
else
|
||||||
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,8 +121,8 @@ public final class CoinFormat {
|
|||||||
if (decimalMark == this.decimalMark)
|
if (decimalMark == this.decimalMark)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,8 +135,8 @@ public final class CoinFormat {
|
|||||||
if (minDecimals == this.minDecimals)
|
if (minDecimals == this.minDecimals)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -146,8 +158,8 @@ public final class CoinFormat {
|
|||||||
List<Integer> decimalGroups = new ArrayList<Integer>(groups.length);
|
List<Integer> decimalGroups = new ArrayList<Integer>(groups.length);
|
||||||
for (int group : groups)
|
for (int group : groups)
|
||||||
decimalGroups.add(group);
|
decimalGroups.add(group);
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift, roundingMode,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift,
|
||||||
codes, codeSeparator, codePrefixed);
|
roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -172,8 +184,8 @@ public final class CoinFormat {
|
|||||||
List<Integer> decimalGroups = new ArrayList<Integer>(repetitions);
|
List<Integer> decimalGroups = new ArrayList<Integer>(repetitions);
|
||||||
for (int i = 0; i < repetitions; i++)
|
for (int i = 0; i < repetitions; i++)
|
||||||
decimalGroups.add(decimals);
|
decimalGroups.add(decimals);
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift, roundingMode,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift,
|
||||||
codes, codeSeparator, codePrefixed);
|
roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -184,8 +196,8 @@ public final class CoinFormat {
|
|||||||
if (shift == this.shift)
|
if (shift == this.shift)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -195,8 +207,8 @@ public final class CoinFormat {
|
|||||||
if (roundingMode == this.roundingMode)
|
if (roundingMode == this.roundingMode)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -206,8 +218,8 @@ public final class CoinFormat {
|
|||||||
if (codes == null)
|
if (codes == null)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, null, codeSeparator, codePrefixed);
|
shift, roundingMode, null, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -224,8 +236,8 @@ public final class CoinFormat {
|
|||||||
if (this.codes != null)
|
if (this.codes != null)
|
||||||
codes.putAll(this.codes);
|
codes.putAll(this.codes);
|
||||||
codes.put(codeShift, code);
|
codes.put(codeShift, code);
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift, roundingMode,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift,
|
||||||
codes, codeSeparator, codePrefixed);
|
roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -237,8 +249,8 @@ public final class CoinFormat {
|
|||||||
if (codeSeparator == this.codeSeparator)
|
if (codeSeparator == this.codeSeparator)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, codePrefixed);
|
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -248,8 +260,8 @@ public final class CoinFormat {
|
|||||||
if (codePrefixed)
|
if (codePrefixed)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, true);
|
shift, roundingMode, codes, codeSeparator, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -259,14 +271,15 @@ public final class CoinFormat {
|
|||||||
if (!codePrefixed)
|
if (!codePrefixed)
|
||||||
return this;
|
return this;
|
||||||
else
|
else
|
||||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||||
roundingMode, codes, codeSeparator, false);
|
shift, roundingMode, codes, codeSeparator, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CoinFormat() {
|
public CoinFormat() {
|
||||||
// defaults
|
// defaults
|
||||||
this.negativeSign = '-';
|
this.negativeSign = '-';
|
||||||
this.positiveSign = 0; // none
|
this.positiveSign = 0; // none
|
||||||
|
this.zeroDigit = '0';
|
||||||
this.decimalMark = '.';
|
this.decimalMark = '.';
|
||||||
this.minDecimals = 2;
|
this.minDecimals = 2;
|
||||||
this.decimalGroups = null;
|
this.decimalGroups = null;
|
||||||
@ -280,11 +293,12 @@ public final class CoinFormat {
|
|||||||
this.codePrefixed = true;
|
this.codePrefixed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CoinFormat(char negativeSign, char positiveSign, char decimalMark, int minDecimals,
|
private CoinFormat(char negativeSign, char positiveSign, char zeroDigit, char decimalMark, int minDecimals,
|
||||||
List<Integer> decimalGroups, int shift, RoundingMode roundingMode, Map<Integer, String> codes,
|
List<Integer> decimalGroups, int shift, RoundingMode roundingMode, Map<Integer, String> codes,
|
||||||
char codeSeparator, boolean codePrefixed) {
|
char codeSeparator, boolean codePrefixed) {
|
||||||
this.negativeSign = negativeSign;
|
this.negativeSign = negativeSign;
|
||||||
this.positiveSign = positiveSign;
|
this.positiveSign = positiveSign;
|
||||||
|
this.zeroDigit = zeroDigit;
|
||||||
this.decimalMark = decimalMark;
|
this.decimalMark = decimalMark;
|
||||||
this.minDecimals = minDecimals;
|
this.minDecimals = minDecimals;
|
||||||
this.decimalGroups = decimalGroups;
|
this.decimalGroups = decimalGroups;
|
||||||
@ -348,6 +362,17 @@ public final class CoinFormat {
|
|||||||
str.append(code());
|
str.append(code());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convert to non-arabic digits
|
||||||
|
if (zeroDigit != '0') {
|
||||||
|
int offset = zeroDigit - '0';
|
||||||
|
System.out.println(offset);
|
||||||
|
for (int d = 0; d < str.length(); d++) {
|
||||||
|
char c = str.charAt(d);
|
||||||
|
if(Character.isDigit(c))
|
||||||
|
str.setCharAt(d, (char) (c + offset));
|
||||||
|
}
|
||||||
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,11 @@ public class CoinFormatTest {
|
|||||||
assertEquals("+1.00", NO_CODE.positiveSign('+').format(Coin.COIN).toString());
|
assertEquals("+1.00", NO_CODE.positiveSign('+').format(Coin.COIN).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDigits() throws Exception {
|
||||||
|
assertEquals("١٢.٣٤٥٦٧٨٩٠", NO_CODE.digits('\u0660').format(Coin.valueOf(1234567890l)).toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecimalMark() throws Exception {
|
public void testDecimalMark() throws Exception {
|
||||||
assertEquals("1.00", NO_CODE.format(Coin.COIN).toString());
|
assertEquals("1.00", NO_CODE.format(Coin.COIN).toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user