在jsf中以2点十进制格式显示数字 [英] Displaying a number in 2 point decimal format in jsf
问题描述
我使用的是 JSF 2 和 RichFaces 3.在下图中,数字显示为数据库中的数字.
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.
但我想将它们显示为 6749395.20
如果有小数部分和 5095138.00
如果没有小数部分.
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>
实际上我将它们全部展示在一起,但我已经尝试将它们作为所有可能的组合使用 type
、groupingUsed
、minFractionDigits
和 pattern
.
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?
推荐答案
如果值不是 Number
,例如 String
.然后,您基本上对它所代表的数据使用了错误的类型.要在 Java 中表示货币,您应该使用 BigDecimal
.另外,请确保数据库表中的类型正确,即它不应是 varchar,而是小数.
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.
一旦您修复了数据类型,<f:convertNumber>
就会按照您的要求工作.请注意,pattern
属性将覆盖 groupingUsed
和 minFractionDigits
.您应该使用 pattern
或 其他的.此外,type="number"
已经是默认值,因此可以将其删除.
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.
所以,要么使用
<f:convertNumber pattern="#0.00" />
或
<f:convertNumber groupingUsed="true" minFractionDigits="2" />
请注意,它们生成不同的格式.您可能希望将分组设置为 false
.
Note that they generate different formats. You probably want to set grouping to false
.
您也可以使用 type="currency"
,然后它会根据 UIViewRoot#getLocale()
自动应用正确的模式:
You can also use type="currency"
, it will then automatically apply the right pattern as per the UIViewRoot#getLocale()
:
<f:convertNumber type="currency" />
另见标签库文档和DecimalFormat
javadoc.
See also the tag library documentation and the DecimalFormat
javadoc.
这篇关于在jsf中以2点十进制格式显示数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!