如何为MessageFormat的所有数字参数设置numberFormat? [英] How do I set the numberFormat for all numeric parameters of a MessageFormat?

查看:103
本文介绍了如何为MessageFormat的所有数字参数设置numberFormat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我必须在不知道元素的情况下使用MessageFormat(模式来自用户输入),但我希望所有的数字都是没有千分隔符。



我试图在所有可用的语言环境中搜索,但所有这些都有千分隔符。

我尝试过创建一个新的Locale,但我无法为他定义一个数字格式。

我试图用一个新的DecimalFormat设置所有元素,将GroupingUsed设置为false,但第一个元素发现是一个非数字元素,抛出异常。



我需要做什么?

我找到的独特解决方案是尝试设置所有参数,尝试在每个参数之后格式化消息。如果收到错误,我必须设置为格式化,但我认为这是一个非常不优雅的解决方案。

Hi all!

I have to use a MessageFormat without knowing the elements (the pattern comes from a user input), but I want that all the numbers would be without the thousand separator.

I tried to search in all available locales, but all of them have the thousand separator.
I tried to create a new Locale, but I wasn't able to define a numberformat for him.
I tried to set ALL elements with a new DecimalFormat having the GroupingUsed setted to false, but the first element found that is a non numeric element, an exception was thrown.

What I have to do?
The unique solution I found is to try to set all parameters trying to format the message after each of that. If an error is catched, I have to set as nothing that format, but I think it's a very inelegant solution.

推荐答案

您是否尝试克隆您的Locale,获取从这里得到Numberformat并在那里设置GroupingUsed(false)?

如果有数字,一个不优雅的解决方案也可以格式化Messageformat之外的数字:



Have you tried to clone your Locale, get the Numberformat from this and set the GroupingUsed(false) there?
An inelegant solution would also to format the numbers outside the Messageformat if there are Numbers:

Object parm = ...
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);

if (parm instanceof Number) {
   parm = nf.format(parm);
}

System.out.println(MessageFormat.format("hello {0}", parm));






谢谢你的回复。



在第一时间我认为这是一个很好的解决方案,但我也有很多已经存在的模式,其中指定了小数位数(并且所有模式都不一样):

{0,数字,0.00}



如果我采用你的解决方案,它抛出一个IllegalArgumentException,因为它等待一个有字符串的数字。



我也尝试设置所有数字元素的所有格式,但是DecimalFormat比格式化模式更强大。即:

Hi,

Thank you for your reply.

In a first moment I thought that it was a good solution, but I have also a lot of already existing patterns where is specified the number of decimals (and it is not the same for all pattern) in this way:
{0,number,0.00}

If I adopt your solution, it throws an IllegalArgumentException, because it wait for a number where there is a string.

I also tried to set all formats for all numeric elements, but the DecimalFormat is stronger than the formatting pattern. Ie:
messageFormat.applyPattern("{0} - {0,number,0.00}");
return messageFormat(new Decimal(12345.6789));



返回


returns

12,345.679 - 12,345.68



这不是我想要的(因为thosuand分组)。



但是:


that is not that I want (because the thosuand grouping).

But:

Object[] params = new Object[] {new Double(12345.6789)};
Format[] fmts = createNumberFormatOnNumericParameters(params);
messageFormat.applyPattern("{0} - {0,number,0.00}");
messageFormat.setFormatsByArgumentIndex(fmts);
return messageFormat(new Decimal(12345.6789));

private Format[] createNumberFormatOnNumericParameters(Object[] params) {
    Format[] result = Format[params.length];
    Format numFormat = NumberFormat.getInstance();
    numFormat.setGroupingUsed(false);
    for (int i = 0; i < params.length; i++ ) {
        if (params[i] instanceOf Number) result[i] = numFormat;
    return result;



返回


returns

12345.679 - 12345.679



这不是我想要的(因为小数位数)。



注意:我不能使用setFormats的setFormat,直到我不知道参数引用的参数的类型而不是参数类型。



这一刻,我adpoted你的解决方案(我没有发现任何已编写的模式,其中有格式化和未格式化的thousanded数字,现在这样:


that is not what I want (because the number of decimals).

Note: I cannot use the setFormat of setFormats until I can't know the types of the arguments that the parameters refer to instead the argument types.

For this moment, I adpoted your solution (I didn't found any already wrote pattern where there is both formatted and unformatted "thousanded" numbers for now), in this way:

try {
    return formatText(pattern, params, true);
} catch (IllegalArgumentException e) {
    return formatText(pattern, params, false);
}



其中formatText方法可选地实现您的解决方案......


where the formatText method implements optionally your solution...


这篇关于如何为MessageFormat的所有数字参数设置numberFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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