3

I use a control to get input as Indian currency. The format which I expect is ##,##,###.##, but I'm not able to achieve this with both locale='hi_IN' or pattern='##,##,###.##'. The value of the control is of type double.

If I change locale to 'hi_IN', digits will be displayed in Devanagari with default thousand separator format(#,###,###.##). Is there a way to achieve INR format?

Selaron
  • 6,105
  • 4
  • 31
  • 39
  • Really interesting - so in `hi_IN` locale the `.` is a decimal separator, and the `,` are what? Hundreds of thousands separators? – Selaron Mar 29 '19 at 14:25
  • 1
    Maybe java experts know how to generally treat this pattern in java - this is why I added the `java` tag. – Selaron Mar 29 '19 at 15:19
  • 1
    Maybe I am missing it but p:inputNumber has properties for decimalSeparator="," thousandSeparator="." see the showcase. https://www.primefaces.org/showcase/ui/input/inputNumber.xhtml – Melloware Mar 29 '19 at 17:04
  • @Melloware the separator separates groups of size 3. The OP needs to separate mixed size groups 2 and 3. While wie say 2,500,000 is 2.5 Million they say it's 25 lakh (a friend explained to me). Knowing this term I found this QA: https://stackoverflow.com/questions/14507658/number-formatting-in-java-to-use-lakh-format-instead-of-million-format - looks like the op needs a custom converter as this is impossible using standard Java API. – Selaron Mar 30 '19 at 05:13

1 Answers1

2

Based on this response to the pure Java variant of your problem I made up a converter using 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();
    }
}

Usage in 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>

Example output screenshot:

enter image description here

Maven Dependency in pom.xml:

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

Example Locale configuration in faces-config.xml:

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

Beware the dependency introduced here is a 12 MByte .jar and it's licensed under Unicode/ICU License. Make sure this is compatible with your project if you use it.

Selaron
  • 6,105
  • 4
  • 31
  • 39