ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-12-11 01:03:21  阅读:284  来源: 互联网

标签:jsf-2-2 jsf primefaces java


我使用控件来获取输入作为印度货币.我期望的格式是##,##,###.##,但是我无法通过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>

输出截图示例:

enter image description here

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.如果使用它,请确保它与您的项目兼容.

标签:jsf-2-2,jsf,primefaces,java
来源: https://codeday.me/bug/20191211/2105433.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有