首页 > 解决方案 > 无法在中显示印度货币格式(##,##,###.##)素面控制

问题描述

我使用控件来获取作为印度货币的输入。我期望的格式是##,##,###.##,但我无法同时使用locale='hi_IN'or来实现这一点pattern='##,##,###.##'。控件的值是 double 类型。

如果我将语言环境更改为'hi_IN',则数字将以默认千位分隔符以梵文显示format(#,###,###.##)。有没有办法实现 INR 格式?

标签: javajsfprimefacesjsf-2.2

解决方案


基于对您的问题的纯 Java 变体的响应,我使用以下方法制作了一个转换器com.ibm.icu.text.DecimalFormat

package my.converter;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Locale;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import com.ibm.icu.text.DecimalFormat;

@FacesConverter("enhancedDecimalConverter")
public class EnhancedDecimalConverter implements Converter<BigDecimal> {

    @Override
    public BigDecimal getAsObject(FacesContext context, UIComponent component, String value) {
        if (null == value) {
            return null;
        }

        DecimalFormat format = getFormatter();
        BigDecimal result;
        try {
            result = BigDecimal.valueOf(format.parse(value).doubleValue());
        } catch (ParseException e) {
            throw new ConverterException(e);
        }

        return result;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, BigDecimal value) {
        if (null == value) {
            return null;
        }

        DecimalFormat format = getFormatter();
        String result = format.format(value);

        return result;
    }

    private DecimalFormat getFormatter() {
        return (DecimalFormat) DecimalFormat.getCurrencyInstance(getLocale());
    }

    private Locale getLocale() {
        return FacesContext.getCurrentInstance().getViewRoot().getLocale();
    }
}

XHTML 中的用法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
    <h:form>
        <p:inputText value="#{myBean.decimalVal}"
            converter="enhancedDecimalConverter">
        </p:inputText>

        <h:panelGrid columns="2">
            <h:outputText value="formatted: " />
            <h:outputText value="#{myBean.decimalVal}"
                converter="enhancedDecimalConverter">
            </h:outputText>

            <h:outputText value="raw: " />
            <h:outputText value="#{myBean.decimalVal}">
            </h:outputText>
        </h:panelGrid>

        <p:commandButton value="submit" process="@form" update="@form" />
    </h:form>
</h:body>
</html>

示例输出截图:

在此处输入图像描述

Maven依赖pom.xml

    <dependency>
        <groupId>com.ibm.icu</groupId>
        <artifactId>icu4j</artifactId>
        <version>64.1</version>
    </dependency>

示例Locale配置faces-config.xml

<application>
    <locale-config>
        <default-locale>hi_IN</default-locale>
    </locale-config>
</application>

注意这里介绍的依赖项是 12 MByte .jar,它是根据Unicode/ICU License获得许可的。如果您使用它,请确保它与您的项目兼容。


推荐阅读