mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-07 06:44:16 +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 positiveSign;
|
||||
private final char zeroDigit;
|
||||
private final char decimalMark;
|
||||
private final int minDecimals;
|
||||
private final List<Integer> decimalGroups;
|
||||
@ -82,8 +83,8 @@ public final class CoinFormat {
|
||||
if (negativeSign == this.negativeSign)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,8 +96,19 @@ public final class CoinFormat {
|
||||
if (positiveSign == this.positiveSign)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
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)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,8 +135,8 @@ public final class CoinFormat {
|
||||
if (minDecimals == this.minDecimals)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,8 +158,8 @@ public final class CoinFormat {
|
||||
List<Integer> decimalGroups = new ArrayList<Integer>(groups.length);
|
||||
for (int group : groups)
|
||||
decimalGroups.add(group);
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift, roundingMode,
|
||||
codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -172,8 +184,8 @@ public final class CoinFormat {
|
||||
List<Integer> decimalGroups = new ArrayList<Integer>(repetitions);
|
||||
for (int i = 0; i < repetitions; i++)
|
||||
decimalGroups.add(decimals);
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift, roundingMode,
|
||||
codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -184,8 +196,8 @@ public final class CoinFormat {
|
||||
if (shift == this.shift)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -195,8 +207,8 @@ public final class CoinFormat {
|
||||
if (roundingMode == this.roundingMode)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -206,8 +218,8 @@ public final class CoinFormat {
|
||||
if (codes == null)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, null, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, null, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,8 +236,8 @@ public final class CoinFormat {
|
||||
if (this.codes != null)
|
||||
codes.putAll(this.codes);
|
||||
codes.put(codeShift, code);
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift, roundingMode,
|
||||
codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -237,8 +249,8 @@ public final class CoinFormat {
|
||||
if (codeSeparator == this.codeSeparator)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, codePrefixed);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, codePrefixed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -248,8 +260,8 @@ public final class CoinFormat {
|
||||
if (codePrefixed)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, true);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -259,14 +271,15 @@ public final class CoinFormat {
|
||||
if (!codePrefixed)
|
||||
return this;
|
||||
else
|
||||
return new CoinFormat(negativeSign, positiveSign, decimalMark, minDecimals, decimalGroups, shift,
|
||||
roundingMode, codes, codeSeparator, false);
|
||||
return new CoinFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups,
|
||||
shift, roundingMode, codes, codeSeparator, false);
|
||||
}
|
||||
|
||||
public CoinFormat() {
|
||||
// defaults
|
||||
this.negativeSign = '-';
|
||||
this.positiveSign = 0; // none
|
||||
this.zeroDigit = '0';
|
||||
this.decimalMark = '.';
|
||||
this.minDecimals = 2;
|
||||
this.decimalGroups = null;
|
||||
@ -280,11 +293,12 @@ public final class CoinFormat {
|
||||
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,
|
||||
char codeSeparator, boolean codePrefixed) {
|
||||
this.negativeSign = negativeSign;
|
||||
this.positiveSign = positiveSign;
|
||||
this.zeroDigit = zeroDigit;
|
||||
this.decimalMark = decimalMark;
|
||||
this.minDecimals = minDecimals;
|
||||
this.decimalGroups = decimalGroups;
|
||||
@ -348,6 +362,17 @@ public final class CoinFormat {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -38,6 +38,11 @@ public class CoinFormatTest {
|
||||
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
|
||||
public void testDecimalMark() throws Exception {
|
||||
assertEquals("1.00", NO_CODE.format(Coin.COIN).toString());
|
||||
|
Loading…
Reference in New Issue
Block a user