货币符号与另一个数字格式 [英] Currency symbol with another number format

查看:497
本文介绍了货币符号与另一个数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个优雅的方式来设置一个数字的货币格式的方式,但保持在另一个实际的数字格式。这是Java。基本上我是这样做的

  NumberFormat format = NumberFormat.getCurrencyInstance(locale); 

这很好,除了我正在为英国用户编写一个系统,我的用户感到沮丧,显示(例如)欧元的数字格式为欧洲人将使用它。因此,英国人50万欧元将会显示为50万欧元(即交换)。我打算将区域设置换成locale.UK,但是我将会有错误的货币符号!



我有几个脏的修复,但我想知道是否有一个优雅的方式来保持与本地语言环境(locale.UK)的区域设置的货币符号数字格式。

解决方案

不同的货币也可以将货币符号放在字符串之前或之后,或者有不同的小数位数(如果有的话)。从你的问题来看,如何处理这些情况并不是很清楚,但是假设你想保留这些差异,试试这个。

不仅仅是交换货币符号转换为本地数字格式,则可以从外部格式开始,并将小数格式符号替换为本地版本。这些还包括货币,所以你必须交换回(不要担心,这是一个副本)。

  public static NumberFormat localStyleForeignFormat(Locale locale){
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
if(格式instanceof DecimalFormat){
DecimalFormat df =(DecimalFormat)格式;
//使用原始货币符号的本地/默认小数符号
DecimalFormatSymbols dfs = new DecimalFormat()。getDecimalFormatSymbols();
dfs.setCurrency(df.getCurrency());
df.setDecimalFormatSymbols(dfs);
}
返回格式;





$ b

这样,您也可以保留货币符号和数字的正确位置的小数位。一些例子,对于默认Locale Locale.UK

  en_GB£500,000.00£500,000.00 
fr_FR 500 000,00€500,000.00€
it_IT€500.000,00€500,000.00
ja_JP¥500,000 JPY500,000
hi_INूू500, 000.00 INR 500,000.00

如果您还想保留外币符号,而不是本地等值,使用

  localDfs.setCurrencySymbol(df.getCurrency()。getSymbol(locale)); 


I was wondering if there was an elegant way to set the currency format of a number in one way but keep the actual number formatting in another. It is Java. Essentially I am doing this

NumberFormat format = NumberFormat.getCurrencyInstance(locale);  

This is largely fine except I am writing a system for UK based users and my users are upset that when showing (for example) euros the number is formatted as europeans would use it. So € 500,000 as a UK person would write it is displaying as € 500.000 (i.e. swap , for .). I was going to swap locale for locale.UK but then I will have the wrong currency symbol!

I have a couple of dirty fixes for this but I wondered if there was an elegant way to keep the currency symbol of the locale with the local locale (locale.UK) number format.

解决方案

Different currencies can also place the currency symbol before or after the string, or have a different number of decimal places (if any). It is not really clear from your question how you want to handle those cases, but assuming you want to preserve those differences, try this.

Instead of just swapping in the currency symbol into your local number format, you could start with the foreign format and substitute the decimal format symbols with your local version. Those also include the currency, so you have to swap that back (don't worry, it's a copy).

public static NumberFormat localStyleForeignFormat(Locale locale) {
    NumberFormat format = NumberFormat.getCurrencyInstance(locale);
    if (format instanceof DecimalFormat) {
        DecimalFormat df = (DecimalFormat) format;
        // use local/default decimal symbols with original currency symbol
        DecimalFormatSymbols dfs = new DecimalFormat().getDecimalFormatSymbols();
        dfs.setCurrency(df.getCurrency());
        df.setDecimalFormatSymbols(dfs);
    }
    return format;
}

This way, you also retain the correct positioning of the currency symbol and the number of decimal places. Some examples, for default-Locale Locale.UK

en_GB   £500,000.00     £500,000.00
fr_FR   500 000,00 €    500,000.00 €
it_IT   € 500.000,00    € 500,000.00
ja_JP   ¥500,000       JPY500,000
hi_IN   रू ५००,०००.००    INR 500,000.00

If you also want to preserve the foreign currency symbol, instead of the local equivalent, use

localDfs.setCurrencySymbol(df.getCurrency().getSymbol(locale));

这篇关于货币符号与另一个数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆