NumberFormat本地化问题

blu*_*sky 2 java localization number-formatting jsf-1.2

我有一个用户输入的金额,想要验证它.我已经编写了一个JSF验证器,但是无法在所有情况下都能使用它.这是我的场景:
我有不同的Locales用户,因此我需要处理各种输入方法,并希望允许以下

English  
1234  
1,234  
1234.56  
1,234.5  

German & Spanish  
1234  
1.234  
1234,56  
1.234,5

French  
1234  
1 234  
1234,56  
1 234,5  
Run Code Online (Sandbox Code Playgroud)

我的问题是法语,因为解析在空格处停止时,使用此代码将选项2和4视为无效.

public void validate(final FacesContext pContext,
                     final UIComponent pComponent,
                     final Object pValue) {

  boolean isValid = true;
  final Locale locale = (Locale)pComponent.getAttributes().get(USERS_LOCALE);
  final Currency currency = (Currency)pComponent.getAttributes().get(CURRENCY);

  final NumberFormat formatter = NumberFormat.getNumberInstance(locale);
  formatter.setGroupingUsed(true);
  formatter.setMinimumFractionDigits(currency.getDefaultFractionDigits());
  formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());
  final ParsePosition pos = new ParsePosition(0);
  final String stringValue = (String)pValue;

  if (pos.getIndex() != stringValue.length() || pos.getErrorIndex() != -1) {
    isValid = false;
  }
  ...
Run Code Online (Sandbox Code Playgroud)

我还想确保以下内容被视为无效,但它们都成功解析(当然除了法语)

1,234,9.56(无效分组)
1,234.567(货币的小数位数太多)

任何帮助将非常感
谢伊恩

Jon*_*oni 5

法国数千个分离器实际上是一个不间断的空间\u00a0.如果您的输入使用常规空间,则可以更改输入:

input = input.replace(' ', '\u00a0');
Run Code Online (Sandbox Code Playgroud)

您可以做的另一件事是将分组符号更改为常规空间:

DecimalFormat decimalFormatter = (DecimalFormat) formatter;
DecimalFormatSymbols symbols = decimalFormatter.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
decimalFormatter.setDecimalFormatSymbols(symbols);
Run Code Online (Sandbox Code Playgroud)

但不能推荐这个.新格式化程序不接受使用非中断空格作为分组字符的数字.