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
How can we do this in java?
Any pointers is highly appreciated.
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
How can we do this in java?
Any pointers is highly appreciated.
As some have pointed out (and I probably should have realized given your example), what you wanted was the integer and fractional parts.
To actually want the exponent and mantissa you can convert it into the IEEE 754 representation and extract the bits like this:
long bits = Double.doubleToLongBits(3.25);
boolean isNegative = (bits & 0x8000000000000000L) != 0;
long exponent = (bits & 0x7ff0000000000000L) >> 52;
long mantissa = bits & 0x000fffffffffffffL;
double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;
[Edit: The question originally asked how to get the mantissa and exponent.]
Where n is the number to get the real mantissa/exponent:
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.
The mantissa and exponent of an IEEE double floating point number are the values such that
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.
Since 1.6, java.lang.Math also provides a direct method to get the unbiased exponent (called 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.
I'd strongly suggest that you don't call these mantissa and exponent.
Google would help you if you used the right terms to search "get fractional and whole part out from double java"
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
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.
Use JSTL (just drop jstl-1.2.jar in /WEB-INF/lib
) fmt taglib. 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.
Here's an SSCCE, just copy'n'paste'n'run it.
<%@ 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
That's it. No need to massage it with help of raw Java code.
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.
<%@ 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>