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);
产出:
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.
// target float point number
double d = 3.025;
// transfer the number to string
DecimalFormat df = new DecimalFormat();
df.setDecimalSeparatorAlwaysShown(false);
String format = df.format(d);
// split the number into two fragments
int dotIndex = format.indexOf(".");
int iPart = Integer.parseInt(format.substring(0, dotIndex)); // output: 3
double fPart = Double.parseDouble(format.substring(dotIndex)); // output: 0.025