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

前端之家收集整理的这篇文章主要介绍了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组成了一个转换器:

  1. package my.converter;
  2. import java.math.BigDecimal;
  3. import java.text.ParseException;
  4. import java.util.Locale;
  5. import javax.faces.component.UIComponent;
  6. import javax.faces.context.FacesContext;
  7. import javax.faces.convert.Converter;
  8. import javax.faces.convert.ConverterException;
  9. import javax.faces.convert.FacesConverter;
  10. import com.ibm.icu.text.DecimalFormat;
  11. @FacesConverter("enhancedDecimalConverter")
  12. public class EnhancedDecimalConverter implements Converter<BigDecimal> {
  13. @Override
  14. public BigDecimal getAsObject(FacesContext context,UIComponent component,String value) {
  15. if (null == value) {
  16. return null;
  17. }
  18. DecimalFormat format = getFormatter();
  19. BigDecimal result;
  20. try {
  21. result = BigDecimal.valueOf(format.parse(value).doubleValue());
  22. } catch (ParseException e) {
  23. throw new ConverterException(e);
  24. }
  25. return result;
  26. }
  27. @Override
  28. public String getAsString(FacesContext context,BigDecimal value) {
  29. if (null == value) {
  30. return null;
  31. }
  32. DecimalFormat format = getFormatter();
  33. String result = format.format(value);
  34. return result;
  35. }
  36. private DecimalFormat getFormatter() {
  37. return (DecimalFormat) DecimalFormat.getCurrencyInstance(getLocale());
  38. }
  39. private Locale getLocale() {
  40. return FacesContext.getCurrentInstance().getViewRoot().getLocale();
  41. }
  42. }

在XHTML中的用法

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  4. xmlns:f="http://xmlns.jcp.org/jsf/core"
  5. xmlns:h="http://xmlns.jcp.org/jsf/html"
  6. xmlns:p="http://primefaces.org/ui">
  7. <h:head />
  8. <h:body>
  9. <h:form>
  10. <p:inputText value="#{myBean.decimalVal}"
  11. converter="enhancedDecimalConverter">
  12. </p:inputText>
  13. <h:panelGrid columns="2">
  14. <h:outputText value="formatted: " />
  15. <h:outputText value="#{myBean.decimalVal}"
  16. converter="enhancedDecimalConverter">
  17. </h:outputText>
  18. <h:outputText value="raw: " />
  19. <h:outputText value="#{myBean.decimalVal}">
  20. </h:outputText>
  21. </h:panelGrid>
  22. <p:commandButton value="submit" process="@form" update="@form" />
  23. </h:form>
  24. </h:body>
  25. </html>

输出截图示例:

enter image description here

pom.xml中的Maven依赖关系:

  1. <dependency>
  2. <groupId>com.ibm.icu</groupId>
  3. <artifactId>icu4j</artifactId>
  4. <version>64.1</version>
  5. </dependency>

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

  1. <application>
  2. <locale-config>
  3. <default-locale>hi_IN</default-locale>
  4. </locale-config>
  5. </application>

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

猜你在找的Java相关文章