JSF outputText and PrimeExtensions inputNumber [duplicate] - jsf

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.

Related

<f:convertNumber /> doesn't affect the footerText value

I have this column:
<p:column headerText="Percent"
footerText="#{bean.entityTotal.percent}">
<h:outputText value="#{entity.percent}">
<f:convertNumber type="percent" />
</h:outputText>
</p:column>
the percent mask(<f:convertNumber type="percent" />) work very well for the <h:outputText>, but the footerText is not affected.
How can i put a mask at this footer without making much effort?
it's not duplicated, because i wanted to know how can i use mask in footerText and the other post doesn't talks about it, talks about using facets.
Your original question was:
How can i put a mask at this footer without making much effort?
Depending on what your definition of 'much' effort is, the answer one of:
Not possible
Masking numbers in PrimeFaces footerText column Datatable
And considering the remarks in the comments, the for you unfortunately unwanted single valid answer is:
Not possible

JSF <h:inputText> should accept just number

i 've been looking a lot about how to make my allows just number to be inserted inside , seems quite easy but it doesn't work in anyway.
I have this code :
<p:fragment id="thresoldPrice">
<h:inputText type="number"value="" disabled="#{!tradingFunnelCBean.tradingFunnelMBean.conditionalOrder}" styleClass="#{!tradingFunnelCBean.tradingFunnelMBean.conditionalOrder ? '' : 'num-col'} middle-space">
<f:convertNumber pattern="#.####" integerOnly="true" maxFractionsDigit="4"/>
<p:watermark for="thresoldPrice"value="#{localizationBean.getMessageValue('EP3.GIMB.TRADINGFUNNEL.PRIVATE.LABEL.WATERMARK', languagePreferenceBean.selectedPreference)}" />
</h:inputText>
</p:fragment>
but it keeps accepting String values. i would also like to make the value inserted in this format "#.####" but the pattern attribute of the h:inputText doesn't make anything.
Any suggestion?

Display amount in format $###,###,###.## using f:convertNumber

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?

Format a JSF converted value

Today I noticed ocpsoft has a nice time library adapted to use in JSF 2 as a converter. The strong point of that is you can use the converter directly in the date displayed in the view and it converts it into a string telling the user something like 6 hours ago or 17 hours from now. I think my best is to combine both, the JSF converted date and this one to display something like 26-03-2013 17:00 (4 hours from now). I can achieve something similar with the following code:
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>
My problem comes when I want to put the second value into parenthesis. The PrettyTimeConverter accepts only a date as a value and I can't write the parenthesis there directly. Also JSF is not accepting the following:
<h:outputFormat value="({0})">
<f:param value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</f:param>
</h:outputFormat>
With that I have the following error:
<f:converter> Parent not an instance of ValueHolder:
javax.faces.component.UIParameter#1492636
Any idea about how to achieve that avoiding writing both parentheses using specific h:outputText tags?
You can just put those parentheses directly in template text without the need for another <h:outputText>s.
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
(<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>)
See also:
Is it suggested to use h:outputText for everything?

Formatting a double in JSF

I have a problem similar to the one found here : JSF selectItem label formatting.
What I want to do is to accept a double as a value for my and display it with two decimals. Can this be done in an easy way?
I've tried using but that seems to be applied on the value from the inputText that is sent to the server and not on the initial value in the input field.
My code so far:
<h:inputText id="december" value="#{budgetMB.december}" onchange="setDirty()" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
</h:inputText>
EDIT: The above code actually works. I was fooled by JDeveloper that didn't update the jsp page even when I did a explicit rebuild of my project and restarted the embedded OC4J server. However, after a reboot of my computer everything was fine.
If I'm not misunderstanding your requirement, I was able to achieve formatting of the value in the input box during the rendering of the view with:
<h:inputText id="text1" value="#{...}">
<f:convertNumber pattern="#,###,##0.00"/>
</h:inputText>
I was using the Standard Faces Components in my vendor-branded Eclipse so I'm assuming the pattern attribute is part of standard JSF.
If what you are trying to do is make the value of the input text field change on screen (to correct user input), you should probably look into using one of the JSF ajax frameworks like Rich Faces.
A possible example would look like this:
<h:inputText id="december" value="#{budgetMB.december}" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
<a4j:support event="onblur" reRender="december" />
</h:inputText>
I haven't tested this, but I think it may work.
It seems you're actually formatting a currency. There already exists a specific formatter to handle currencies that you can assign many options to:
<f:convertNumber type="currency" />
Some interesting attributes of this tag are: locale, currencyCode, integerOnly, currencySymbol and pattern.

Resources