How do I get whole and fractional parts from double in JSP/Java?

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?


double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;

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.