If your local currency idoesn'thave 2 decimal places
then it won't be handled properly by the version 0.80
mercator build.
The problem:
Format.toMoney() always divides by 100
The solution:
Get a java.until.Currency instance and check how many
DPs the currency uses. In the case of Japanese Yen,
this is 0, so don' divide. Arabic currencies (so I've
heard) use 3 decimal places, so divide by 1000.
Sample code:
//Iain 2004-03-18 since we'll use the defaults more
than 99% of the time, let's initialize them once and re-
use them
static private java.text.DecimalFormat defaultFormat
= (java.text.DecimalFormat)
java.text.NumberFormat.getCurrencyInstance
(Application.locale());
static private java.util.Currency defaultCurrency =
java.util.Currency.getInstance(Application.locale());
/**
* Locale sensitive money formating.
* Returns a string representation of a numeric
amount formatted according to the specified locale.
* NOTE that the current implementation may not be
100% appropriate.
* When dealing with foreign currencies, we want to
dispplay the number in the default(local) format,
* but use the correct locale to obtain information
about the currency such as the number of decimal places
* and the name of or symbol for the the currency,
eg $,\, DM etc
* Probably, the number instance should ALWAYS be
based on the Application.locale(), with the
* Currency instance being able to be based on the
locale provided as a parameter.
*/
public static String toMoney(String s, java.util.Locale
locale) {
java.text.DecimalFormat useFormat; // number
format to use this time. usually the default
java.util.Currency useCurrency; // currency
instance to use this time
if (locale == null)
locale = Application.locale();
if (locale == Application.locale()) {
// use the defaults
useFormat = defaultFormat;
useCurrency = defaultCurrency;
} else {
// use the specified locale to initialze format
and currency instances to use this time
useFormat = (java.text.DecimalFormat)
java.text.NumberFormat.getCurrencyInstance(locale);
useCurrency =
java.util.Currency.getInstance(locale);
}
Double val = new Double(s);
//java.text.DecimalFormat format =
(java.text.DecimalFormat)
java.text.NumberFormat.getCurrencyInstance(locale);
if (moneyFormat != null){
useFormat.applyPattern(moneyFormat);
}
switch (useCurrency.getDefaultFractionDigits
()) {
case 0:
return (useFormat.format
(val.doubleValue())); // eg; japanese yen
case 1:
return (useFormat.format
(val.doubleValue() / 10.0)); // ???
case 2:
return (useFormat.format
(val.doubleValue() / 100.0)); // dollars, euros, etc
case 3:
return (useFormat.format
(val.doubleValue() / 1000.0)); // arabic currency -
apparently
}
return (useFormat.format(val.doubleValue() /
100.0));
//return (format.format(val.doubleValue()));
}
Something like that.
Logged In: YES
user_id=972075
PosPrinter class should use Format.toMoney()
It currently has hardcoded statements like:
Double.toString(itemTender.amount()/100)