一尘不染

如何在JSP / Java中从double中获得全部和分数部分?

jsp

如何在JSP / Java中从double获得全部和分数部分?如果值为3.25,那么我想得到fractional =.25whole = 3

我们如何用Java做到这一点?


阅读 268

收藏
2020-06-08

共1个答案

一尘不染

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);

输出:

Integer part = 2
Fractional part = 0.2999999999999998
2020-06-08