人名币符号java代码,Java中特定货币的货币符号位置

I know the way to get the Currency Object and other details for a currency in java using locale and NumberFormat class.

But I am not able to find anything in the API to know whether currency symbol is display at start or end

E.g. 10 in US is $10 where $ is in the start of number)

10 in Zloty (polish currency) is 10 z (z to represent zloty symbol though actual symbol is different).

Is there any property in number format or currency class which can help me find whether currency symbol is placed in start or end?

解决方案

I don't seem to have very many Locales loaded... but france uses a trailing symbol, taiwan uses a leading symbol.

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);

}

}

Now if you really want to know if the currency symbol is at the beginning or the end, use the following as a starting point. Note the bPre variable...

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 "???";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值