Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Double keeps rounding to integer?

    I'm working with an audiostream, using the clip.getMicrosecondPosition() method to find out how many microseconds have passed.
    When left alone, the microsecond value is dead on precise - but when I try to multiply it and convert to to a decimal (double), it always rounds to the nearest integer.

    Here's the code:
    Java Code:
    double seconds = clip.getMicrosecondPosition() / 1000000;
    The integer number it rounds to is correct for how many seconds have passed, but I need the double to show the tenth-second as well. For instance, when 1000000 microseconds have passed, it will show the double as 1.0, but when 1324752 have passed, it still shows 1.0 rather than 1.3. I've tried a few decimal formatting and double parsing tricks and everything else I could find/think of, but so far nothing is working.

    Help would be much appreciated!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Double keeps rounding to integer?

    The expression to the right of the = is all int values so int rules are used. Change the divisor to double by adding a .0D to the end to get a floating point value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Double keeps rounding to integer?

    Cheers!

    Here's the final code:

    Java Code:
    seconds = clip.getMicrosecondPosition() / 1000000.0D;
    seconds = (double) Math.round(seconds * 10) / 10;

Similar Threads

  1. convert integer to double
    By himanwish in forum New To Java
    Replies: 4
    Last Post: 03-17-2011, 01:55 AM
  2. Adding an integer to a double?
    By tokoolio in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 06:57 PM
  3. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  4. How to extract integer value out of a double value?
    By javaexplorer in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2010, 05:32 AM
  5. rounding a double in a number of significant numbers
    By rikribbers in forum Advanced Java
    Replies: 2
    Last Post: 10-27-2008, 03:35 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
  •