本文整理匯總了Java中java.util.Currency.getDefaultFractionDigits方法的典型用法代碼示例。如果您正苦於以下問題:Java Currency.getDefaultFractionDigits方法的具體用法?Java Currency.getDefaultFractionDigits怎麽用?Java Currency.getDefaultFractionDigits使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Currency
的用法示例。
在下文中一共展示了Currency.getDefaultFractionDigits方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: adjustForCurrencyDefaultFractionDigits
import java.util.Currency; //導入方法依賴的package包/類
/**
* 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);
}
}
}
}
示例2: format
import java.util.Currency; //導入方法依賴的package包/類
/**
* Formats Money into a human readable currency string.
*
* @param moneyOrNull the money to format, or null
* @param locale the {@link Locale} to format for
* @return a formatted money string, or null if moneyOrNull is null
*/
public static String format(Money moneyOrNull, Locale locale) {
if (moneyOrNull == null) {
return null;
}
// Convert the currency specified in the proto to a Currency object.
Currency currency = Currency.getInstance(moneyOrNull.getCurrency().toString());
// Create a formatter that uses the currency.
NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
formatter.setCurrency(currency);
formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());
if (currency.getDefaultFractionDigits() == 0) {
// For locales that do not support fractional amounts, use the amount as is. For example,
// Japan has no concept of "cents". The base unit of currency is 1 Yen, and currency is always
// measured in Yen.
return formatter.format(moneyOrNull.getAmount());
} else {
// For locales that support fractional amounts, divide by 100 to get the local equivalent of
// "dollars". For example, the amount in USD is represented as cents. We devide by 100 to get
// US dollars.
return formatter.format(moneyOrNull.getAmount() / 100.0);
}
}
示例3: MoneyLabel
import java.util.Currency; //導入方法依賴的package包/類
public MoneyLabel(BigDecimal value, Currency currency, boolean showSymbol, boolean showCode, boolean html)
{
super(value);
final int frac = currency.getDefaultFractionDigits();
if( frac != -1 )
{
setMinDecimals(frac);
setMaxDecimals(frac);
}
this.currency = currency;
this.showSymbol = showSymbol;
this.showCode = showCode;
this.html = html;
}
示例4: testCurrencyDefined
import java.util.Currency; //導入方法依賴的package包/類
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());
}
}
示例5: next2
import java.util.Currency; //導入方法依賴的package包/類
public float next2(DataContext context) {
Currency currency = Currency.getInstance(Locale.getDefault());
float cents = 0;
if (currency.getDefaultFractionDigits() == 2) {
if (useCommon) {
cents = common[random.nextInt(common.length)];
} else {
cents = (float) random.nextInt(100) / 100;
}
}
return Math.min(max, random.nextInt((int) Math.floor(max + 1)) + cents);
}
示例6: getExponent
import java.util.Currency; //導入方法依賴的package包/類
private static int getExponent(@NonNull String currencyCode) {
// first some overrides:
if ("ISK".equals(currencyCode)) {
return 2;
}
if ("CLP".equals(currencyCode)) {
return 2;
}
if ("MXP".equals(currencyCode)) {
return 2;
}
if ("MRO".equals(currencyCode)) {
return 1;
}
if ("IDR".equals(currencyCode)) {
return 0;
}
if ("VND".equals(currencyCode)) {
return 0;
}
if ("UGX".equals(currencyCode)) {
return 0;
}
if ("CVE".equals(currencyCode)) {
return 0;
}
if ("ZMW".equals(currencyCode)) {
return 2;
}
if ("GHC".equals(currencyCode)) {
return 0;
}
if ("BYR".equals(currencyCode)) {
return 0;
}
if ("BYN".equals(currencyCode)) {
return 2;
}
if ("RSD".equals(currencyCode)) {
return 2;
}
// now the default behavior
Currency curr = null;
try {
curr = Currency.getInstance(currencyCode);
} catch (final IllegalArgumentException exception) {
Log.d(TAG, "Currency is incorrect: ", exception);
}
int exponent = 0;
if (curr != null) {
exponent = curr.getDefaultFractionDigits();
if (exponent == -1) {
exponent = 0;
}
}
return exponent;
}
示例7: getPerLitreFractionDigits
import java.util.Currency; //導入方法依賴的package包/類
public static int getPerLitreFractionDigits(Currency currency) {
checkPropertiesAreLoaded();
if (properties.containsKey(currency.getCurrencyCode()))
return Integer.valueOf(properties.getProperty(currency.getCurrencyCode()).split(DELIMETER)[FRACTIONS_PER_LITRE]);
return currency.getDefaultFractionDigits() + 1;
}
示例8: Money
import java.util.Currency; //導入方法依賴的package包/類
/**
* The constructor does not complex computations and requires simple, inputs
* consistent with the class invariant. Other creation methods are available
* for convenience.
*/
public Money(BigDecimal amount, Currency currency) {
if (amount.scale() != currency.getDefaultFractionDigits()) throw new IllegalArgumentException("Scale of amount does not match currency");
this.currency = currency;
this.amount = amount;
}
示例9: testFormatting
import java.util.Currency; //導入方法依賴的package包/類
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();
}
}