用符号解析货币:并非所有情况都适用-Java [英] Parse currency with symbol: Not all case working - Java

查看:111
本文介绍了用符号解析货币:并非所有情况都适用-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 java.text.NumberFormat 解析具有货币符号的String货币。

  NumberFormat.getCurrencyInstance(Locale.FRANCE).parse( 1 599, 99€); //失败
NumberFormat.getCurrencyInstance(Locale.FRANCE).parse( 599,99€); //可以正常运行

能否有人解释一下为什么它在第一种情况下不起作用?
Joda-Money是一种更好的此类解析库吗?

解决方案

此方法可能不适用于的原因您,是无效的空白字符的用法。



格式类希望代码为160的char被描述为不间断空格,而当您传递代码30时,该字符为空格。 p>

您可以运行以下代码进行检查。

  NumberFormat currencyInstance = NumberFormat .getCurrencyInstance(Locale.FRANCE); 

String France = 1 599,99€;
字符串格式= currencyInstance.format(1599.99);
数字number = currencyInstance.parse(格式);


System.out.println( France: + format);
System.out.println( Format: + format);
System.out.println( Number: +数字);

if(france.equals(format)){
number = currencyInstance.parse(france); //必须工作;
System.out.println(number);
} else {

char [] fr = france.toCharArray();
char [] ft = format.toCharArray();

for(int i = 0; i< fr.length; i ++){
if(fr [i]!= ft [i]){
System.out .printf(位置%d上的值不相等。法国:%d;格式:%d,i,(int)fr [i],(int)ft [i]);
}
}

}

然后环境详细信息:

  java版本 1.7.0_17 
Java SE运行时环境(内部版本1.7.0_17- b02)
Java HotSpot(TM)64位服务器VM(内部版本23.7-b01,混合模式)
操作系统:Windows 7 64位。

ASCII表



注意:



由于@Black Panter添加了这个已知的错误



JDK-4510618:[Fmt-Nu]法语成千上万分隔符是不间断的空格


周围的工作



( 1)删除要解析
的字符串中用户的分组分隔符(即U + 0020)。



(2)将用户的分组分隔符替换为格式化程序的分组
分隔符(U + 00A0),如果不方便删除所有空格。



I am using java.text.NumberFormat to parse String currency which has currency symbol. It is working for some cases while fails for other.

NumberFormat.getCurrencyInstance(Locale.FRANCE).parse("1 599,99 €"); //fails
NumberFormat.getCurrencyInstance(Locale.FRANCE).parse("599,99 €"); //works fine

Can somebody please explain why it is not working in first case? Is Joda-Money a better library for such type of parsing?

解决方案

The reason that this might not work for you, is usage of invalid white space character.

The format class expect the char with code 160 witch is describe as "Non-breaking space", when you may pass the code 30 witch is "Space".

You can run this code to check it.

NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.FRANCE);

String france = "1 599,99 €";
String format = currencyInstance.format(1599.99);
Number number = currencyInstance.parse(format);


System.out.println("France: " + format);
System.out.println("Format: " + format);
System.out.println("Number: " + number);

if(france.equals(format)) {
    number = currencyInstance.parse(france); // Must work;
    System.out.println(number);
} else {

    char[] fr = france.toCharArray();
    char[] ft = format.toCharArray();

    for(int i = 0; i < fr.length; i++) {
        if(fr[i] != ft[i]) {
            System.out.printf("The value on position %d are not equal. France: %d; Format: %d", i,(int)fr[i], (int)ft[i]);
        }
    }

}

Then environment details:

 java version "1.7.0_17"
 Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
 Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
 OS: Windows 7 64-bits. 

ASCII table

NOTE:

As @Black Panter added this is known bug

JDK-4510618 : [Fmt-Nu] French thousands separator is non-breaking space

WORK AROUND

(1) Remove user's grouping separator (i.e. U+0020) in a String to be parsed.

(2) Replace user's grouping separator with the formatter's grouping seperator (U+00A0) if it's inconvenient to remove all spaces.

这篇关于用符号解析货币:并非所有情况都适用-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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