user1487380
user1487380

Reputation: 362

Format decimal places for primefaces ext input numeric

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]

Upvotes: 1

Views: 10333

Answers (4)

grigouille
grigouille

Reputation: 705

p:inputNumber is hard to use with a variable length of digits. Just use p:inputText with a large maxFractionDigits and force the locale to get the right decimal separator and thousand separator.

<p:inputText value="#{myBean.myVal}">
  <f:convertNumber minFractionDigits="1" maxFractionDigits="50"
    locale="#{myBean.locale}"/>
  <f:validateDoubleRange minimum="0" maximum="999999999999999.999999999"/>
</p:inputText>

Upvotes: 0

Santiago Perez
Santiago Perez

Reputation: 1

Just use the tag padControl="false" with the inputNumber control

<p:inputNumber style="width:140px;"
    value="#{mopEdicionBean.permisoMop.mopKlmtFinalB}"
    thousandSeparator="" decimalPlaces="3" maxlength="4" minValue="0"
    maxValue="9999" size="16" padControl="false">
    <p:ajax event="change" update="nombre_permiso"/>
</p:inputNumber>

Upvotes: 0

user1487380
user1487380

Reputation: 362

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=","
    />

Upvotes: 2

MaQy
MaQy

Reputation: 476

You can use decimalPlaces attribute. The default value is obtained from the maxValue attribute, so in your case it is 999999999999999.999999999.

Upvotes: 0

Related Questions