Results 1 to 6 of 6
  1. #1
    arnab321 is offline Member
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    Angry decimal calculations?

    hi,
    when i try this: float a=p.getDuration()/1000000;
    System.out.println("a="+a+" actual="+p.getDuration());

    p.getDuration() is a number like 1791800
    so, a is expected to be 1.7918.
    but why does is come as 1.0?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Is that getDuration() return an int value? In that case you have to cast it as a float.

    Java Code:
    float a = (float)p.getDuration()/1000000;

  3. #3
    arnab321 is offline Member
    Join Date
    Oct 2008
    Posts
    40
    Rep Power
    0

    Default

    tried that just now. it still returns 1.0. getDuration returns a long

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    you need something like:

    Java Code:
    float a = (float)(p.getDuration()/1000000.0);
    Your use of an integer in the denominator causes the division of "p.getDuration()" to be done as an integer.

    Floating an integer is still an integer

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 11:04 PM
  3. Why my program cannot calculate the decimal value?
    By pearllymary78 in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 12:52 AM
  4. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 09:28 PM

Posting Permissions

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