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?
To get the decimal part you could do this: double original = 1.432d; double decimal = original % 1d; Note that due to the way that decimals are actually thought of, this might kill any precision you were after.
For nonnegative real numbers, the fractional part is just the "part of the number after the decimal," e.g. { 3.64 } = 3.64 − ⌊ 3.64 ⌋ = 3.64 − 3 = 0.64.
Nonintegral Numeric Types Nonintegral data types are those that represent numbers with both integer and fractional parts. The nonintegral numeric data types are Decimal (128-bit fixed point), Single Data Type (32-bit floating point), and Double Data Type (64-bit floating point).
For example, 1.3 = 1 + 0.3. The integer is called the integer part of x, whereas the decimal is called the fractional part of x (or sometimes the decimal part of x). This representation is not unique. For example, you can also write 1.3 = 2 + (-0.7).
double value = 3.25; double fractionalPart = value % 1; double integralPart = value - fractionalPart;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With