java-无法显示素数控制中的印度货币格式(##,##,###.##)

本文介绍了一种在Java中使用ICU4J库实现印度货币(INR)格式输入的方法,通过创建自定义的JSF转换器EnhancedDecimalConverter,实现了对数字的正确格式化和解析,适用于使用Devanagari字符集的印度地区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我使用控件来获取输入作为印度货币.我期望的格式是##,##,###.##,但是我无法通过locale =’hi_IN’或pattern =’##,##,###.#来实现. #’.控件的值为double类型.

 

如果我将语言环境更改为“ hi_IN”,则数字将以默认的千位分隔符格式(#,###,###.##)在Devanagari中显示.
有没有办法实现INR格式?

最佳答案

基于this response to the pure Java variant of your problem,我使用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>

输出截图示例:

 

pom.xml中的Maven依赖关系:

 

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

faces-config.xml中的示例语言环境配置:

 

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值