3 Replies Latest reply on Sep 19, 2006 4:07 AM by 807598

    How to avoid the round up from decimals?

    807598
      Hello, I'm trying to transform a given time in seconds to the format of Hours:minutes:seconds.milliseconds. My output is correct, only that I don't want to see decimals, but if I switch the decimals off I get an automatic round up which I dont want, let me show the code here:

      public class Time
      {
      public static void main(String[] args)
      {
      double time = 4484.71; //total race time in seconds

      double StoH = 0.000277777; //Convertion factor from Seconds to Hours
      double HtoM = 60; //Convertion factor from Hours to Minutes
      double MtoS = 60; //Convertion factor from Minutes to Seconds
      double StoMS = 1000; //Convertion factor from Seconds to Milliseconds

      double Hours = time * StoH; //gets the total time in hours
      String extractMfromH = Double.toString(Hours); //transforms Hours to a string
      String getMinutes = extractMfromH.substring(2,10); //gets the decimal part from the total time
      double toMinutes = Double.parseDouble(getMinutes); //transforms the decimal part to a double
      double Minutes = (toMinutes/100000000) * HtoM; //converts the remaining part to minutes
      String extractSfromM = Double.toString(Minutes); //transforms Minutes to a string
      String getSeconds = extractSfromM.substring(3,10); //gets the decimal part from the minutes
      double toSeconds = Double.parseDouble(getSeconds); //transforms the decimal part to a double
      double Seconds = (toSeconds/10000000) * MtoS; //converts the decimal part to seconds
      String extractMSfromS = Double.toString(Seconds); //transforms Seconds to a string
      String getMilliseconds = extractMSfromS.substring(3,10); //gets the decimal part from the seconds
      double toMilliseconds = Double.parseDouble(getMilliseconds); //transforms the decimal part to a double
      double Milliseconds = (toMilliseconds/10000000) * StoMS; //converts the decimal part to milliseconds

      System.out.printf("%3.0fh " , Hours);
      System.out.printf(" %2.1fm " , Minutes);
      System.out.printf(" %2.1fs " , Seconds);
      System.out.printf(" %3.1fms " , Milliseconds);
      }
      }

      My output is this: 1h 14.7m 44.7s 697.4ms

      I need to get rid of the decimals WITHOUT rounding up the numbers, how can I do that?

      By the way, I also don't know why I'm getting a different time for the milliseconds, 697ms, the game shows 755ms, if someone wants to try to convert this 4484.71seconds to H:MM:SS.mss and gets 755ms instead of the 697ms that I'm getting, please tell me how you got it.

      Thanks