how is it possible to display a double like "1"
At the moment the double appears with 1.0
But I want to have for example 1,5, too.
But if I have only 1 (the numbers after the comma or dot is zero, than I want to have a value without comma or dot)
How can I do this?
Thanks
I suppose this is what you want to achieve:
<h:outputText value="1,0">
<f:convertNumber minFractionDigits="0" />
</h:outputText>
Will output 1
<h:outputText value="1,5">
<f:convertNumber minFractionDigits="0" />
</h:outputText>
Will output 1,5
Make sure to use correct decimal separator depending on your Locale when testing this.
You can play arround a bit more with the f:convertNumber tag. http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_convertNumber.html
Related
I'm having a inputText where a number having a comma as a decimal separator is expected. The backend amount field is a BigDecimal, so the actual number going to the backend should be in the form 100.00 while I want the UI to show 100,00 and not accept 100.00.
I have looked at the inputNumber component which kinda does what I want, but we do not have that dependency in our project, so it's out of discussion.
I have also tried adding <f:convertNumber pattern="#0,00" />, but that does not help or I don't know how to use it...
Any help would be appreciated.
<p:inputText id="grossPayment"
styleClass="greyText boldText"
converterMessage="#{nts['grossPaymentNumbers']}"
requiredMessage="#{nts['grossPaymentRequired']}"
required="#{bean.profile.salary.required}"
value="#{bean.profile.salary.amount}">
<f:validator
disabled="#{!bean.profile.salary.required}"
validatorId="positiveNumberValidator" />
</p:inputText>
I am using JSF 2 and RichFaces 3. Here in the picture shown below, numbers are being displayed as what they are in the database.
But I want to display them as 6749395.20 if fraction part is there and 5095138.00 if no fraction part is there.
As of now I have tried something like this.
<rich:column>
<f:facet name="header">
<h:outputText value="Total Amount"/>
</f:facet>
<h:outputText value="#{rr[2]}">
<f:convertNumber type="number" groupingUsed="true" minFractionDigits="2" pattern="#0.00"/>
</h:outputText>
</rich:column>
Actually I am showing all of them together, but I have tried with all of them as all possible combinations with type, groupingUsed, minFractionDigits and pattern.
Why does it not work? How is this caused and how can I solve it?
That can happen if the value is not a Number at all, for example a String. You're then basically using the wrong type for the data it represents. To represent currencies in Java, you should be using BigDecimal. Also, make sure that the type in the database table is right, i.e. it should not be a varchar, but a decimal.
Once you've fixed the data type, then the <f:convertNumber> will work as you told it to do. Note that the pattern attribute will override the groupingUsed and minFractionDigits. You should use either the pattern or the others. Also, type="number" is already the default, so it can be removed.
So, either use
<f:convertNumber pattern="#0.00" />
or
<f:convertNumber groupingUsed="true" minFractionDigits="2" />
Note that they generate different formats. You probably want to set grouping to false.
You can also use type="currency", it will then automatically apply the right pattern as per the UIViewRoot#getLocale():
<f:convertNumber type="currency" />
See also the tag library documentation and the DecimalFormat javadoc.
I want to restrict my <p:inputMask> with only letters and numbers without giving any size (max-legth) limit. I tried to use mask attribute but when I use mask attriibute it gives a max-length for my inputMask. Could you please help me to solve this issue ?
Sample Code :
<p:inputMask value="#{gercekKrediBasvuruDetayGirisView.kisi.ticariHarfSeri}"
required="true" mask="***"
requiredMessage="#{msg['GercekKrediBasvuruTuketiciMaliBilgiler.belgeSeriNull']}"
/>
If your input does not have to be <p:inputMask> and can be <h:inputText> or <p:inputText> then the easiest way to go would be to use it with regex:
<h:inputText id="inputField" value="#{backingbean.username}" validatorMessage="Value does not match pattern.">
<f:validateRegex pattern="^[a-zA-Z0-9]+$" />
</h:inputText>
<h:message for="inputField" />
If you only allow lower case letters go with ^[a-z0-9]+$.
<p:inputMask> is designed for somewhat stricter validation than only letters and numbers.
I would like to display the amount in $12,050,999.00 format.
I tried as follows:
<h:outputText value="#{sampleBean.Amount}">
<f:convertNumber pattern="###,###" currencySymbol="$" type="currency"/>
</h:outputText>
However, it didn't display the amount in the desired format. I got 12,050,999 instead.
The desired format is shown in the below image:
How can I achieve this?
Your pattern is wrong for a currency. You should be using pattern="¤#,##0.00".
<f:convertNumber pattern="¤#,##0.00" currencySymbol="$" />
However, there's more at matter: in your original code you also specified the type attribute, which is correct, but this is mutually exclusive with the pattern attribute whereby the pattern attribute gets precedence.
You should actually be omitting the pattern attribute and stick to the type attribute.
<f:convertNumber type="currency" currencySymbol="$" />
Note that this uses the locale as available by UIViewRoot#getLocale() which is expected to be an English/US based locale in order to get the right final format for the USD currency. You'd like to explicitly specify it in either the <f:view>:
<f:view locale="en_US">
or in the locale attribute of the <f:convertNumber>:
<f:convertNumber type="currency" currencySymbol="$" locale="en_US" />
See also:
Does <f:convertNumber> use the right number separator when using patterns to format currency?
I want to know how to set the default decimal separator on my JSF application. I have some <h:inputText> that I need to format as money, with 2 decimals. Right now the decimal separator used by default is the comma , and this gives me an error when I do some operations on save. I don't know if I can set the decimal separator to be used everytime that I use <f:convertNumber> tag.
I tried to use this:
<f:convertNumber pattern="########0.00" groupingUsed="false" />
but it still sets the comma as decimal separator.
The default decimal separator depends on the locale used. You can set it in 2 ways:
On a per-view basis by the locale attribute of the <f:view> tag:
<f:view locale="#{bean.locale}">
On a per-converter basis by the locale attribute of the <f:convertNumber> tag:
<f:convertNumber locale="#{bean.locale}" />
It's unclear what locale you're targeting, but the use of . as fraction separator is typical for US dollars with a locale of en-US, for example. So you need to set it as such:
<f:convertNumber type="currency" currencySymbol="$" locale="en-US" />
It can also be obtained from a java.util.Locale bean property.
<f:convertNumber type="currency" currencySymbol="$" locale="#{bean.locale}" />
Note that I used type="currency", that's more self-documenting.
See also:
Does <f:convertNumber> use the right number separator when using patterns to format currency?
Localization in JSF, how to remember selected locale per session instead of per request/view