java JSP中如何控制显示位数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3904505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to control of display the number of digit in JSP?
提问by Questions
I want to ask a question about the JSP and Java. I am writing the JSP page and has a bean class. In the class, i have a getter function which will return a double var in the jsp page. I used the following code to display the number.
我想问一个关于JSP和Java的问题。我正在编写 JSP 页面并且有一个 bean 类。在课堂上,我有一个 getter 函数,它将在 jsp 页面中返回一个双变量。我使用以下代码来显示数字。
<p> the value of AB is <%=obj.getValue() %> <p>
I expect that display will like the following
我希望显示器会喜欢以下内容
the value of AB is 0.45
However, the real display is the following
但是,真正的显示如下
the value of AB is 0.4569877987
I only want to display the last two digit. What should I do in order to display what I want. Should I modify the JSP page or the Java class variable? thank you.
我只想显示最后两位数。我该怎么做才能显示我想要的东西。我应该修改 JSP 页面还是 Java 类变量?谢谢。
采纳答案by Grodriguez
You can use a DecimalFormat
to format the value as a string, e.g.:
您可以使用 aDecimalFormat
将值格式化为字符串,例如:
DecimalFormat df = new DecimalFormat("#0.00");
(the pattern assumes you always want two digits in the fractional part, even if the fractional part is zero. Otherwise, use #0.##
)
(该模式假设您总是希望小数部分中有两位数字,即使小数部分为零。否则,使用#0.##
)
However in general it is recommended not to call the DecimalFormat
constructors directly. Instead, the recommended practice is to call one of the factory methods of NumberFormat
, then customize the pattern as needed, e.g.:
但是一般情况下建议不要DecimalFormat
直接调用构造函数。相反,推荐的做法是调用 的工厂方法之一NumberFormat
,然后根据需要自定义模式,例如:
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
Javadoc including examples:
Javadoc 包括示例:
- http://download-llnw.oracle.com/javase/6/docs/api/java/text/NumberFormat.html
- http://download-llnw.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
- http://download-llnw.oracle.com/javase/6/docs/api/java/text/NumberFormat.html
- http://download-llnw.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
This should work in your JSP page:
这应该适用于您的 JSP 页面:
<%@ page import="java.text.NumberFormat" %>
<%
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
%>
[...]
<p> the value of AB is <%= nf.format(obj.getValue()) %> <p>
Also see Bozho's answer for a way to achieve the same result using JSTL.
另请参阅 Bozho 的回答,了解使用 JSTL 实现相同结果的方法。
回答by Bozho
In JSP you can do this using the JSTL tag <fmt:formatNumber>
. You can set either a pettern
(#0.##
) or maxFractionDigits="2"
在 JSP 中,您可以使用 JSTL 标记来做到这一点<fmt:formatNumber>
。您可以设置pettern
( #0.##
) 或maxFractionDigits="2"
Avoid using Java code in JSPs - use JSTL instead. See this tutorial. And see this question
回答by Andrzej Doyle
To answer your last question first - you should modify the JSP page, since you want to change how the number is displayed, not what the number actually is.
首先回答您的最后一个问题 - 您应该修改 JSP 页面,因为您想要更改数字的显示方式,而不是数字实际是什么。
To do this, instead of invoking the number's default toString method, you could use a DecimalFormatobject to convert the number to text in whatever format you want. In this case, you probably want to use:
为此,您可以使用DecimalFormat对象将数字转换为您想要的任何格式的文本,而不是调用数字的默认 toString 方法。在这种情况下,您可能想要使用:
new DecimalFormat("#0.##")
as the format string.
作为格式字符串。
However, as per Bozho's answerthere is a JSTL tag to achieve this, which is a better way to solve your problem.
但是,根据 Bozho 的回答,有一个 JSTL 标签可以实现这一点,这是解决问题的更好方法。