我知道如何使用 locale 和 NumberFormat 类获取 java 中货币的货币对象和其他详细信息。
但我无法在 API 中找到任何内容来了解货币符号是在开始还是结束时显示
例如。在美国,10 表示 10 美元,其中 $ 位于数字开头)
10 的兹罗提(波兰货币)为 10 z(z 代表兹罗提符号,但实际符号不同)。
是否有任何数字格式或货币类别的属性可以帮助我查找货币符号是否放置在开始或结束位置?
我似乎没有加载很多语言环境...但法国使用尾随符号,台湾使用前导符号。
public class MyCurrency {
public static void main(String[] args) {
System.out.println(format(Locale.FRANCE, 1234.56f));
System.out.println(format(Locale.TAIWAN, 1234.56f));
}
public static String format(Locale locale, Float value) {
NumberFormat cfLocal = NumberFormat.getCurrencyInstance(locale);
return cfLocal.format(value);
}
}
现在,如果您确实想知道货币符号是在开头还是结尾,请使用以下内容作为起点。请注意bPre
多变的...
public String format(Locale locale, Float value) {
String sCurSymbol = "";
boolean bPre = true;
int ndx = 0;
NumberFormat cfLocal = NumberFormat.getCurrencyInstance(locale);
if (cfLocal instanceof DecimalFormat) { // determine if symbol is prefix or suffix
DecimalFormatSymbols dfs =
((DecimalFormat) cfLocal).getDecimalFormatSymbols();
sCurSymbol = dfs.getCurrencySymbol();
String sLP = ((DecimalFormat) cfLocal).toLocalizedPattern();
// here's how we tell where the symbol goes.
ndx = sLP.indexOf('\u00A4'); // currency sign
if (ndx > 0) {
bPre = false;
} else {
bPre = true;
}
return cfLocal.format(value);
}
return "???";
}
信用 - 我从这个页面撕下了代码。http://www.jguru.com/faq/view.jsp?EID=137963 http://www.jguru.com/faq/view.jsp?EID=137963
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)