Thread: How to extract integer value out of a double value?

1. Member
Join Date
Jan 2010
Posts
20
Rep Power
0

How to extract integer value out of a double value?

Hi,

Suppose the double value is 10.27, and I want to extract the integer part (10) out of it. Then, how I go about doing this?

Thanks

2. Member
Join Date
Dec 2009
Location
Germany
Posts
43
Rep Power
0
you can cast it to int

double d = 10.27;
int a = (int) d;

3. Member
Join Date
Jan 2010
Posts
20
Rep Power
0
Thanks! It didn't occur to me :)

4. Senior Member
Join Date
Aug 2009
Location
Pittsburgh, PA
Posts
285
Rep Power
12
Casting to int will truncate the result.
12.2 => 12; -12.2 => -12; 12.6 => 12; -12.6 => -12

To get rounding to nearest integer use (int)Math.round(float-or-double-value)
12.2 => 12; -12.2 => -12; 12.6 => 13; -12.6 => -13

5. Member
Join Date
Jan 2010
Posts
20
Rep Power
0
Thanks for your input! However, my immediate requirement was to extract the integer part only without rounding it off.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•