为什么 JSTL 使用 ${dollarAmount} 将双精度值 99 输出为 99.0(而不是 99.00)?
我有一个 double
值 dollarAmount
,其值为 99
:
double dollarAmount = 99;
当我尝试使用此代码
<c:out value="${dollarAmount}"/>
将 其输出到我的页面时: 99.0
。
因为它代表财务价值,所以我想将其输出为
99
或 as
99.00
有没有某种方法可以强制
执行此操作?
I have a double
value dollarAmount
which has the value 99
:
double dollarAmount = 99;
When I try to output it to my page using this code:
<c:out value="${dollarAmount}"/>
It comes out as 99.0
.
Because it represents a financial value, I want to output it either as
99
or as
99.00
Is there some way to force <c:out>
to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当将后端数据打印到 HTML 响应时,所有非 String java 对象默认都会转换为
String
。 HTML 不能以其他方式表示。您看到的是99.0
,因为这是Double#toString(double)
。格式按照文档。要格式化货币,最好使用
< fmt formatNumber>
,其type
设置为currency
。它将显示为
When printing backend data to HTML response, all non-String java objects are by default converted to
String
. HTML can namely not be represented on other way. You're seeing99.0
, because that's the default result ofDouble#toString(double)
. The formatting is as per the documentation.To format currencies, better use
<fmt formatNumber>
whosetype
is set tocurrency
.It'll show up as
我建议使用 FormatNumber 标签 fmt
之类的东西
i suggest the use of FormatNumber tag fmt
something like
使用
fmt:formatNumber
Use
fmt:formatNumber
在 java 中,打印特定数量的数字的最佳方法是
printf
,这是来自 https 的第三方实用程序: //sharkysoft.com/ 可在 GNU GPL 下使用,或付费用于商业用途。这是 JavaDoc 页面: http:// /sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htmIn java the best way to print out specific amounts of digits is
printf
, a third-party utility from https://sharkysoft.com/ available under the GNU GPL, or for a fee for commercial use. Here is the JavaDoc page: http://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm