We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Converting a float to minutes and seconds

on
Medium Priority
316 Views
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.

Comment
Watch Question

## View Solutions Only

Commented:
long l = 520;

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

Not the solution you were looking for? Getting a personalized solution is easy.

CERTIFIED EXPERT
Top Expert 2016

Commented:
int mins = (int)(f / 60);
int secs = (int)(f % 60);

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

long l = (long) _any_float_here_
CERTIFIED EXPERT
Top Expert 2016
Commented:

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

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile