下面列出了java.util.Currency#getDefaultFractionDigits ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
void adjustForCurrencyDefaultFractionDigits() {
Currency currency = symbols.getCurrency();
if (currency == null) {
try {
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
} catch (IllegalArgumentException e) {
}
}
if (currency != null) {
int digits = currency.getDefaultFractionDigits();
if (digits != -1) {
int oldMinDigits = getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == getMaximumFractionDigits()) {
setMinimumFractionDigits(digits);
setMaximumFractionDigits(digits);
} else {
setMinimumFractionDigits(Math.min(digits, oldMinDigits));
setMaximumFractionDigits(digits);
}
}
}
}
/**
* Returns a string representing the currency value such as "3.14,USD" for
* a CurrencyValue of $3.14 USD.
*/
public String strValue() {
int digits = 0;
try {
Currency currency =
Currency.getInstance(this.getCurrencyCode());
if (currency == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"Invalid currency code " + this.getCurrencyCode());
}
digits = currency.getDefaultFractionDigits();
}
catch(IllegalArgumentException exception) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"Invalid currency code " + this.getCurrencyCode());
}
String amount = Long.toString(this.getAmount());
if (this.getAmount() == 0) {
amount += "000000".substring(0,digits);
}
return
amount.substring(0, amount.length() - digits)
+ "." + amount.substring(amount.length() - digits)
+ "," + this.getCurrencyCode();
}
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
private static void adjustForCurrencyDefaultFractionDigits(
DecimalFormat format, DecimalFormatSymbols symbols) {
Currency currency = symbols.getCurrency();
if (currency == null) {
try {
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
} catch (IllegalArgumentException e) {
}
}
if (currency != null) {
int digits = currency.getDefaultFractionDigits();
if (digits != -1) {
int oldMinDigits = format.getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == format.getMaximumFractionDigits()) {
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
} else {
format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
format.setMaximumFractionDigits(digits);
}
}
}
}
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
private static void adjustForCurrencyDefaultFractionDigits(
DecimalFormat format, DecimalFormatSymbols symbols) {
Currency currency = symbols.getCurrency();
if (currency == null) {
try {
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
} catch (IllegalArgumentException e) {
}
}
if (currency != null) {
int digits = currency.getDefaultFractionDigits();
if (digits != -1) {
int oldMinDigits = format.getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == format.getMaximumFractionDigits()) {
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
} else {
format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
format.setMaximumFractionDigits(digits);
}
}
}
}
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
private static void adjustForCurrencyDefaultFractionDigits(
DecimalFormat format, DecimalFormatSymbols symbols) {
Currency currency = symbols.getCurrency();
if (currency == null) {
try {
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
} catch (IllegalArgumentException e) {
}
}
if (currency != null) {
int digits = currency.getDefaultFractionDigits();
if (digits != -1) {
int oldMinDigits = format.getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == format.getMaximumFractionDigits()) {
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
} else {
format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
format.setMaximumFractionDigits(digits);
}
}
}
}
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
private static void adjustForCurrencyDefaultFractionDigits(
DecimalFormat format, DecimalFormatSymbols symbols) {
Currency currency = symbols.getCurrency();
if (currency == null) {
try {
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
} catch (IllegalArgumentException e) {
}
}
if (currency != null) {
int digits = currency.getDefaultFractionDigits();
if (digits != -1) {
int oldMinDigits = format.getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == format.getMaximumFractionDigits()) {
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
} else {
format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
format.setMaximumFractionDigits(digits);
}
}
}
}
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
private static void adjustForCurrencyDefaultFractionDigits(
DecimalFormat format, DecimalFormatSymbols symbols) {
Currency currency = symbols.getCurrency();
if (currency == null) {
try {
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
} catch (IllegalArgumentException e) {
}
}
if (currency != null) {
int digits = currency.getDefaultFractionDigits();
if (digits != -1) {
int oldMinDigits = format.getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == format.getMaximumFractionDigits()) {
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
} else {
format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
format.setMaximumFractionDigits(digits);
}
}
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private void init(){
this.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
currentLocale = retrieveLocale();
Currency currentCurrency = getCurrencyForLocale(currentLocale);
decimalDigits = currentCurrency.getDefaultFractionDigits();
initCurrencyTextWatcher();
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
private static void testCurrencyDefined(String currencyCode, int digits) {
Currency currency = Currency.getInstance(currencyCode);
if (currency.getDefaultFractionDigits() != digits) {
throw new RuntimeException("[" + currencyCode
+ "] expected: " + digits
+ "; got: " + currency.getDefaultFractionDigits());
}
}
static void testFormatting() {
boolean failed = false;
Locale[] locales = {
Locale.US,
Locale.JAPAN,
Locale.GERMANY,
Locale.ITALY,
new Locale("it", "IT", "EURO") };
Currency[] currencies = {
null,
Currency.getInstance("USD"),
Currency.getInstance("JPY"),
Currency.getInstance("DEM"),
Currency.getInstance("EUR"),
};
String[][] expecteds = {
{"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
{"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
{"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
{"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
{"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
};
for (int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
for (int j = 0; j < currencies.length; j++) {
Currency currency = currencies[j];
String expected = expecteds[i][j];
if (currency != null) {
format.setCurrency(currency);
int digits = currency.getDefaultFractionDigits();
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
}
String result = format.format(1234.56);
if (!result.equals(expected)) {
failed = true;
System.out.println("FAIL: Locale " + locale
+ (currency == null ? ", default currency" : (", currency: " + currency))
+ ", expected: " + expected
+ ", actual: " + result);
}
}
}
if (failed) {
throw new RuntimeException();
}
}
static void testFormatting() {
boolean failed = false;
Locale[] locales = {
Locale.US,
Locale.JAPAN,
Locale.GERMANY,
Locale.ITALY,
new Locale("it", "IT", "EURO") };
Currency[] currencies = {
null,
Currency.getInstance("USD"),
Currency.getInstance("JPY"),
Currency.getInstance("DEM"),
Currency.getInstance("EUR"),
};
String[][] expecteds = {
{"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
{"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
{"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
{"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
{"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
};
for (int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
for (int j = 0; j < currencies.length; j++) {
Currency currency = currencies[j];
String expected = expecteds[i][j];
if (currency != null) {
format.setCurrency(currency);
int digits = currency.getDefaultFractionDigits();
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
}
String result = format.format(1234.56);
if (!result.equals(expected)) {
failed = true;
System.out.println("FAIL: Locale " + locale
+ (currency == null ? ", default currency" : (", currency: " + currency))
+ ", expected: " + expected
+ ", actual: " + result);
}
}
}
if (failed) {
throw new RuntimeException();
}
}
static void testFormatting() {
boolean failed = false;
Locale[] locales = {
Locale.US,
Locale.JAPAN,
Locale.GERMANY,
Locale.ITALY,
new Locale("it", "IT", "EURO") };
Currency[] currencies = {
null,
Currency.getInstance("USD"),
Currency.getInstance("JPY"),
Currency.getInstance("DEM"),
Currency.getInstance("EUR"),
};
String[][] expecteds = {
{"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
{"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
{"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
{"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
{"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
};
for (int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
for (int j = 0; j < currencies.length; j++) {
Currency currency = currencies[j];
String expected = expecteds[i][j];
if (currency != null) {
format.setCurrency(currency);
int digits = currency.getDefaultFractionDigits();
format.setMinimumFractionDigits(digits);
format.setMaximumFractionDigits(digits);
}
String result = format.format(1234.56);
if (!result.equals(expected)) {
failed = true;
System.out.println("FAIL: Locale " + locale
+ (currency == null ? ", default currency" : (", currency: " + currency))
+ ", expected: " + expected
+ ", actual: " + result);
}
}
}
if (failed) {
throw new RuntimeException();
}
}