Converting a float to minutes and seconds

Posted on 2006-04-04
Medium Priority
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
  • 3
  • 2
LVL 14

Assisted Solution

StillUnAware earned 400 total points
ID: 16374875
long l = 520;

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

Expert Comment

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

Expert Comment

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

long l = (long) _any_float_here_
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Accepted Solution

CEHJ earned 1600 total points
ID: 16374899
Formatted per your style:

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

Author Comment

ID: 16375043
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

ID: 16375221

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

862 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