Converting a float to minutes and seconds

Posted on 2006-04-04
Last Modified: 2012-05-05
Hey there!

This is a quick question really.

I have a float (lets say 520) which represents the number of seconds in a journey.

I'm wanting to give a time for that journey in seconds (in the format xx minutes xx seconds) so in this case (520 seconds), it would be "8 minutes 40 seconds".

Are there a few simple lines of code that can do this please?

I've had a Google at it but I'm mostly getting back API stuff on Time and Date.

Thanks in advance.
Question by:DanBAtkinson
    LVL 14

    Assisted Solution

    long l = 520;

    String time = ""+(l/60)+"m "+(l%60)+"s";
    LVL 86

    Expert Comment

    int mins = (int)(f / 60);
    int secs = (int)(f % 60);
    LVL 14

    Expert Comment

    oh, You said float, then simply cast it to long:

    long l = (long) _any_float_here_
    LVL 86

    Accepted Solution

    Formatted per your style:

    System.out.printf("%d minutes %d seconds\n", (int)(f /60), (int)(f % 60));

    Author Comment

    I'd like to split the points here to CEHJ because they supplied an answer which was in the style I asked and it's in one line.

    But I'll also award points to StillUnAware for the speed and the fact that his is also correct.

    100 to StillUnAware as an assist
    400 to CEHJ for the correct answer
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now