Format decimal places for primefaces ext input numeric

user1487380 picture user1487380 · Jun 21, 2013 · Viewed 7.8k times · Source

I have some code as below (work fine)

  <pe:inputNumber
        value="#{myBean.myVal}"
        minValue="0" maxValue="999999999999999.999999999"
        emptyValue="1"
        thousandSeparator="."
        decimalSeparator=","
    />

then, if i enter [1] it will show [1,000000000], my trouble is alot of zero maybe confuse my user. Is there anyway to trim it on this input ?, just display whatever user entered such as [1,20] -> [1,2] or [1,20]

Answer

user1487380 picture user1487380 · Jun 24, 2013

my script :

function trimDecimalPlace(){                
                var curVal = $(".ui-inputNum > input:first-child").val();
                var parseVal = '';
                if(curVal.indexOf(".") > curVal.indexOf(",")){
                    var tokens = curVal.split(".");
                    var decimalVal = parseFloat("0."+tokens[1])+"";                 
                    if(decimalVal != "0"){
                       parseVal = tokens[0]+"."+decimalVal.substring(2);
                    }else{
                       parseVal = tokens[0];
                    }   
                }else{
                    var tokens = curVal.split(",");
                    var decimalVal = parseFloat("0."+tokens[1])+"";                 
                    if(decimalVal != "0"){
                       parseVal = tokens[0]+","+decimalVal.substring(2);
                    }else{
                       parseVal = tokens[0];
                    }
                }
                $(".ui-inputNum > input:first-child").val(parseVal);
            }

and call it on blur:

    <pe:inputNumber
        value="#{myBean.myVal}"
        minValue="0" maxValue="999999999999999.999999999"
        emptyValue="1"
        onblur="trimDecimalPlace();"
        thousandSeparator="."
        decimalSeparator=","
    />