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

2022-08-31 09:31:33

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

我们如何在Java中做到这一点?


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

答案 2

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