如何在 JSP/Java 中从 double 中获取整数部分和小数部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/343584/
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 do I get whole and fractional parts from double in JSP/Java?
提问by Vinayak Bevinakatti
How do I get whole and fractional parts from double in JSP/Java ? If the value is 3.25 then I want to get fractional =.25
, whole = 3
如何在 JSP/Java 中从 double 中获取整数部分和小数部分?如果值是 3.25 那么我想得到fractional =.25
,whole = 3
How can we do this in Java?
我们如何在 Java 中做到这一点?
采纳答案by Gishu
http://www.java2s.com/Code/Java/Data-Type/Obtainingtheintegerandfractionalparts.htm
http://www.java2s.com/Code/Java/Data-Type/Obtainingtheintegerandfractionalparts.htm
double num;
long iPart;
double fPart;
// Get user input
num = 2.3d;
iPart = (long) num;
fPart = num - iPart;
System.out.println("Integer part = " + iPart);
System.out.println("Fractional part = " + fPart);
Outputs:
输出:
Integer part = 2
Fractional part = 0.2999999999999998
回答by Rasmus Faber
The original question asked for the exponent and mantissa, rather than the fractional and whole part.
最初的问题要求指数和尾数,而不是分数和整数部分。
To get the exponent and mantissa from a double you can convert it into the IEEE 754representation and extract the bits like this:
要从 double 中获取指数和尾数,您可以将其转换为IEEE 754表示形式并提取如下位:
long bits = Double.doubleToLongBits(3.25);
boolean isNegative = (bits & 0x8000000000000000L) != 0;
long exponent = (bits & 0x7ff0000000000000L) >> 52;
long mantissa = bits & 0x000fffffffffffffL;
回答by Dan Vinton
double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;
回答by Stephen Darlington
[Edit: The question originally asked how to get the mantissa and exponent.]
[编辑:问题最初是问如何获得尾数和指数。]
Where nis the number to get the real mantissa/exponent:
其中n是获得真实尾数/指数的数字:
exponent = int(log(n))
mantissa = n / 10^exponent
Or, to get the answer you were looking for:
或者,要获得您正在寻找的答案:
exponent = int(n)
mantissa = n - exponent
These are not Java exactly but should be easy to convert.
这些不完全是 Java,但应该很容易转换。
回答by Pete Kirkham
The mantissa and exponent of an IEEE double floating point number are the values such that
IEEE 双浮点数的尾数和指数是这样的值:
value = sign * (1 + mantissa) * pow(2, exponent)
if the mantissa is of the form 0.101010101_base 2 (ie its most sigificant bit is shifted to be after the binary point) and the exponent is adjusted for bias.
如果尾数的形式为 0.101010101_base 2(即其最重要的位被移动到二进制小数点之后)并且指数被调整为偏差。
Since 1.6, java.lang.Math also provides a direct method to get the unbiased exponent (called getExponent(double))
从 1.6 开始,java.lang.Math 还提供了直接获取无偏指数的方法(称为 getExponent(double))
However, the numbers you're asking for are the integral and fractional parts of the number, which can be obtained using
但是,您要求的数字是数字的整数部分和小数部分,可以使用
integral = Math.floor(x)
fractional = x - Math.floor(x)
though you may you want to treat negative numbers differently (floor(-3.5) == -4.0)
, depending why you want the two parts.
尽管您可能希望以不同的方式对待负数(floor(-3.5) == -4.0)
,这取决于您想要这两个部分的原因。
I'd strongly suggest that you don't call these mantissa and exponent.
我强烈建议您不要将这些尾数和指数称为。
回答by Gishu
public class MyMain2 {
public static void main(String[] args) {
double myDub;
myDub=1234.5678;
long myLong;
myLong=(int)myDub;
myDub=(myDub%1)*10000;
int myInt=(int)myDub;
System.out.println(myLong + "\n" + myInt);
}
}
回答by BalusC
Since this 1-year old question was kicked up by someone who corrected the question subject, and this question is been tagged with jsp, and nobody here was able to give a JSP targeted answer, here is my JSP-targeted contribution.
由于这个1岁的问题是被纠正问题主题的人踢出来的,并且这个问题被标记为jsp,并且这里没有人能够给出JSP针对性的答案,这是我的JSP针对性的贡献。
Use JSTL(just drop jstl-1.2.jarin /WEB-INF/lib
) fmttaglib. There's a <fmt:formatNumber>
tag which does exactly what you want and in a quite easy manner with help of maxFractionDigits
and maxIntegerDigits
attributes.
使用JSTL(只需将jstl-1.2.jar放入/WEB-INF/lib
)fmttaglib。有一个<fmt:formatNumber>
标签可以在maxFractionDigits
和maxIntegerDigits
属性的帮助下以非常简单的方式完成您想要的操作。
Here's an SSCCE, just copy'n'paste'n'run it.
这是一个SSCCE,只需复制'n'paste'n'run它。
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
// Just for quick prototyping. Don't do this in real! Use servlet/javabean.
double d = 3.25;
request.setAttribute("d", d);
%>
<!doctype html>
<html lang="en">
<head>
<title>SO question 343584</title>
</head>
<body>
<p>Whole: <fmt:formatNumber value="${d}" maxFractionDigits="0" />
<p>Fraction: <fmt:formatNumber value="${d}" maxIntegerDigits="0" />
</body>
</html>
Output:
输出:
Whole: 3
Fraction: .25
整体:3
分数:0.25
That's it. No need to massage it with help of raw Java code.
就是这样。无需借助原始 Java 代码对其进行处理。
回答by Roland Illig
Since the fmt:formatNumber
tag doesn't always yield the correct result, here is another JSP-only approach: It just formats the number as string and does the rest of the computation on the string, since that is easier and doesn't involve further floating point arithmetics.
由于fmt:formatNumber
标记并不总是产生正确的结果,这是另一种仅限 JSP 的方法:它只是将数字格式化为字符串并对字符串进行其余的计算,因为这更容易并且不涉及进一步的浮点算术。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
double[] numbers = { 0.0, 3.25, 3.75, 3.5, 2.5, -1.5, -2.5 };
pageContext.setAttribute("numbers", numbers);
%>
<html>
<body>
<ul>
<c:forEach var="n" items="${numbers}">
<li>${n} = ${fn:substringBefore(n, ".")} + ${n - fn:substringBefore(n, ".")}</li>
</c:forEach>
</ul>
</body>
</html>
回答by OnePunchMan
Main logic you have to first find how many digits are there after the decimal point.
This code works for any number upto 16 digits. If you use BigDecimal you can run it just for upto 18 digits. put the input value (your number) to the variable "num", here as an example i have hard coded it.
主要逻辑你必须首先找到小数点后有多少位数字。
此代码适用于最多 16 位的任何数字。如果您使用 BigDecimal,则最多只能运行 18 位数字。将输入值(您的数字)放入变量“num”中,作为示例,我对其进行了硬编码。
double num, temp=0;
double frac,j=1;
num=1034.235;
// FOR THE FRACTION PART
do{
j=j*10;
temp= num*j;
}while((temp%10)!=0);
j=j/10;
temp=(int)num;
frac=(num*j)-(temp*j);
System.out.println("Double number= "+num);
System.out.println("Whole part= "+(int)num+" fraction part= "+(int)frac);
回答by QED
Don't know if this is faster but I'm using
不知道这是否更快,但我正在使用
float fp = ip % 1.0f;