I have Requirement to format a number based on INDIA locale. I have tried locale's like

ar_IN
bn_IN
hi_IN
en_IN with function pxFormatNumber.
is there any function to format number based on INDIA Locale or Currency.

  • Hi Varaaparna, I too tried pxFormatNumber function and some OOTB Java functions by passing INDIA locale, But it doesn't seem to format in the Indian way.

    So, I wrote a custom function with the below code to achieve this. Run this function if the operator locale is India.

    
    String orignalNo = s;
        String formatted = "";
    
        if(orignalNo.startsWith("-")) {
            s = s.replace("-","");
        }
    
        if(orignalNo.contains(".")) {
            if(s.length() > 6){
                StringBuilder sb=new StringBuilder(s);
                s = sb.reverse().toString();
    
                formatted = s.substring(0,6);
                s = s.substring(6);
    
                while(s.length() > 1) {
                    formatted += "," + s.substring(0,2);
                    s = s.substring(2);
                }
    
                sb = new StringBuilder(formatted+(StringUtils.isNotBlank(s) ? ","+s :""));
                formatted = sb.reverse().toString();
            } else {
                formatted = s;
            }
        } else {
            if(s.length() > 3){
                StringBuilder sb=new StringBuilder(s);
                s = sb.reverse().toString();
    
                formatted = s.substring(0,3);
                s = s.substring(3);
    
                while(s.length() > 1) {
                    formatted += "," + s.substring(0,2);
                    s = s.substring(2);
                }
    
                sb = new StringBuilder(formatted+(StringUtils.isNotBlank(s) ? ","+s :""));
                formatted = sb.reverse().toString();
            } else {
                formatted = s;
            }
        }
    
        if (orignalNo.startsWith("-")){
            formatted = "-"+formatted;
        }
    
        return formatted;

    Configuration of Function :

    Output

    This function accepts String and returns a String. you can also concatenate the Rupee symbol to the output in DT if needed.

    I hope it helps in your scenario !

Hi Varaaparna, I too tried pxFormatNumber function and some OOTB Java functions by passing INDIA locale, But it doesn't seem to format in the Indian way.

So, I wrote a custom function with the below code to achieve this. Run this function if the operator locale is India.


String orignalNo = s;
    String formatted = "";

    if(orignalNo.startsWith("-")) {
        s = s.replace("-","");
    }

    if(orignalNo.contains(".")) {
        if(s.length() > 6){
            StringBuilder sb=new StringBuilder(s);
            s = sb.reverse().toString();

            formatted = s.substring(0,6);
            s = s.substring(6);

            while(s.length() > 1) {
                formatted += "," + s.substring(0,2);
                s = s.substring(2);
            }

            sb = new StringBuilder(formatted+(StringUtils.isNotBlank(s) ? ","+s :""));
            formatted = sb.reverse().toString();
        } else {
            formatted = s;
        }
    } else {
        if(s.length() > 3){
            StringBuilder sb=new StringBuilder(s);
            s = sb.reverse().toString();

            formatted = s.substring(0,3);
            s = s.substring(3);

            while(s.length() > 1) {
                formatted += "," + s.substring(0,2);
                s = s.substring(2);
            }

            sb = new StringBuilder(formatted+(StringUtils.isNotBlank(s) ? ","+s :""));
            formatted = sb.reverse().toString();
        } else {
            formatted = s;
        }
    }

    if (orignalNo.startsWith("-")){
        formatted = "-"+formatted;
    }

    return formatted;

Configuration of Function :

Output

This function accepts String and returns a String. you can also concatenate the Rupee symbol to the output in DT if needed.

I hope it helps in your scenario !

@duraisanka, thank you for much for quick reply
I have few queries.
1.will above script work without giving locale into the operator id.(Requirements is like need to format the numbers based on different countries,i can't keep India locale in operator id)
2.in pega 7.1,7,when i create function,giving below error

in library i gave API for stringUtils.

    varaaparna Here are the answers to your questions on the above solution:

    1. The above code doesn't depend on the locale of the operator. It accepts the amount string and returns a value. When you are working on multiple locales, you can run this function when needed for the operators by restricting by when condition.
    2. I did this in the Pega 8.4 instance. I didn't import any additional classes for this function. But you can try adding java.lang.* , java.util.* and java.text.* in the library rule.

    Happy to help you! Let me know if you have further queries 😃

      In 8.4, it worked with above script.
      In 7.1.7, isNotBlank method is not listed under StringUtiles in Pega API instead they provided isBlank method.
      So i have replaced with IsBlank method,which is absolutely working well.

      A very big thanks to @duraisankar and OneStopPega
      my issue is resolved within a hours.

      Write a Reply...

      -An Initiative Of OneStopPega- Love our work? Support Us🖤