服务器设置正在覆盖DecimalFormat [英] DecimalFormat is being overridden by server settings

查看:159
本文介绍了服务器设置正在覆盖DecimalFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在显示格式化小数时遇到问题。在我的本地机器上,我有一个十进制值:0.002100000000存储在数据库中。

Currently I'm having a problem displaying formatted decimals. In my local machine I have a decimal value: 0.002100000000 stored in database.

<h:outputText value="0.002100000000" converter="#{bigDecimal4DigitsConverter}" />

@FacesConverter("bigDecimal4DigitsConverter")
public class BigDecimal4DigitsConverter extends BigDecimalConverter {

    private DecimalFormat format = new DecimalFormat("#,##0.0000");

    @Override
    protected DecimalFormat getDecimalFormat() {
        return format;
    }
}

我的问题出现在我的本地机器上:
0.0021 - 美国设置
但是在另一台服务器上
0,0021 - 法语设置

My problem is on my local machine it displays: 0.0021 - US Settings But in another server 0,0021 - French Settings

为什么?我认为DecimalFormat,无论语言环境如何都格式化十进制值?

Why is that? I thought DecimalFormat, formats a decimal value regardless of locale?

推荐答案

DecimalFormat 模式就是它的名字(和javadoc)说,一个纯粹的模式。在此模式中,表示分组分隔符,表示小数分隔符。它就像 MMM 代表 SimpleDateFormat (请注意,它不会返回 MMM 格式化时的月份,但就像可能或例如 Mei ,具体取决于区域设置)。

The DecimalFormat pattern is, as its name (and javadoc) says, a pure pattern. In this pattern, the , represents the grouping separator and the . represents the decimal separator. It's exactly like as that MMM represents the abbreviated month in SimpleDateFormat (note that it doesn't return MMM as month during formatting, but just like May or e.g. Mei depending on the locale).

在格式化过程中用作分组分隔符和小数分隔符的实际字符(以及用作缩写月份的实际文本)取决于区域设置,与您观察到的完全一样。这是正确的行为。如果在创建 DecimalFormat (或 SimpleDateFormat )期间未显式指定语言环境,则默认语言环境可用通过 将假定区域设置#getDefault() 。您应该实际指定 UIViewRoot#getLocale(),或者如果您的JSF Web指定了固定的语言环境,例如 Locale.ENGLISH 由于某些不明原因,应用程序未本地化。

The actual character being used as grouping separator and decimal separator (and the actual text being used as abbreviated month) during formatting depends on the locale, exactly as you observed. This is correct behavior. When you don't explicitly specify the locale during creating the DecimalFormat (or SimpleDateFormat), then the default locale as available by Locale#getDefault() will be assumed. You should actually be specifying the UIViewRoot#getLocale() or maybe a fixed locale like Locale.ENGLISH if your JSF web application is not localized for some unclear reason.

请注意 DecimalFormat 是(如 SimpleDateFormat )不是线程安全的(检查javadoc中的同步部分)。您不应该在类/实例范围中创建它,而是在线程局部范围中创建它(即在您需要它的方法块中)。

Please also note that DecimalFormat is (like SimpleDateFormat) not threadsafe (check the "Synchronization" section in the javadoc). You should not be creating it in class/instance scope, but in thread local scope (i.e. in the very same method block as where you need it).

我不知道你正在使用哪个 BigDecimalConverter 标准的JSF一个没有 getDecimalFormat()方法,所以我不能给出正确方法的更具体的例子。

I have only no idea which BigDecimalConverter you're using, the standard JSF one doesn't have a getDecimalFormat() method, so I can't give a more concrete example of the proper approach.

这篇关于服务器设置正在覆盖DecimalFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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