java - 无法在中显示印度货币格式(##,##,###.##)素面控制
问题描述
我使用控件来获取作为印度货币的输入。我期望的格式是##,##,###.##
,但我无法同时使用locale='hi_IN'
or来实现这一点pattern='##,##,###.##'
。控件的值是 double 类型。
如果我将语言环境更改为'hi_IN'
,则数字将以默认千位分隔符以梵文显示format(#,###,###.##)
。有没有办法实现 INR 格式?
解决方案
基于对您的问题的纯 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获得许可的。如果您使用它,请确保它与您的项目兼容。
推荐阅读
- java - 如何处理 WebTables 层中的 iframe
- jquery - 查明 jQuery 验证是通过 element() 还是表单提交启动的
- php - Laravel7 Passport:尝试将请求发送到“api/user”时获取“未定义索引:aud”
- wpf - Prism WPF (Unity) - 注册执行以创建对象的表达式
- java - Flutter如何解析JSON Mongodb数据
- java - 我正在尝试解决 Hacke 等级问题 - Sherlock 和有效字符串,但我的编码在 2 个测试用例中失败
- c# - 组合框绑定到 MVVM 中的 ObservableCollection 的问题
- c - 在 C 中的数组中插入了一个不同的值
- r - 如何定义可能不使用数据框中的某些参数?
- javascript - 计算文本频道、语音频道、所有频道和角色