Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

math

jsp

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?

like image 710
Vinayak Bevinakatti Avatar asked Dec 05 '08 11:12

Vinayak Bevinakatti


People also ask

How do you extract the decimal part of a number 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.

How do you find the fractional part of a number?

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.

What is the type of the number containing integral and fractional parts both in Java?

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

How do you write a fractional part?

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


1 Answers

double value = 3.25; double fractionalPart = value % 1; double integralPart = value - fractionalPart; 
like image 73
Dan Vinton Avatar answered Sep 18 '22 00:09

Dan Vinton