Results 1 to 5 of 5
  1. #1
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Question 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. #2
    jogep is offline Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    43
    Rep Power
    0

    Default

    you can cast it to int

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

  3. #3
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    Thanks! It didn't occur to me :)

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    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. #5
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    Thanks for your input! However, my immediate requirement was to extract the integer part only without rounding it off.

Similar Threads

  1. How to Extract?
    By KB1SYV in forum New To Java
    Replies: 2
    Last Post: 11-11-2009, 06:51 AM
  2. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 02:51 PM
  3. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  4. Extract A tar.gz file
    By sajdutt in forum Advanced Java
    Replies: 3
    Last Post: 03-03-2009, 09:29 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM

Posting Permissions

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