• Log in or sign up for an account here: https://www.sythe.org/login

Converting minutes to seconds in C?

SexayMistahBee

Sexiest Bee On Earth

$50 USD Donor New
Joined
Feb 28, 2006
Sythe Gold
27
Messages
2,410
Age
32
Location
South Korea
I'm trying to convert the decimals of minutes as a float to seconds.

So if it were 50.429 minutes, it would be 50 minutes and 25 seconds

I've tried converting the float value to int and subtracting it from the original float and multiplying it by 60

ex)
((50.429-(int)50.429)*60)

but this gives me a 26 seconds instead of 25.

I really need to get back to the basics; what's going on here?

Is there a better way to do this? (without using floor)

thanks :/
 
Supporting Business
Discord Voice
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
PHP Shit Show 2k26
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
but this gives me a 26 seconds instead of 25.

I really need to get back to the basics; what's going on here?

Is there a better way to do this? (without using floor)

thanks :/

You can emulate the behaviour of floor using integer division.

Code:
	float input = 50.429;
	int m = input / 1;
	int s = ((input - m) * 60) / 1;
	printf("%d %d", m, s);

Look up "integer division" in C for details.
 
Thanks, dividing by 1 helped clear up my code a bit.

This was for a simple programming assignment, and here's the product :p

Code:
#include <stdio.h>

int main(void)
{
	float input;
	int hour, min, sec;

	printf("How many minutes did the trains take to meet?\n");
	scanf("%f",&input);

	hour = (int)input/60;
	min = ((int)input/1)%60;
	sec = ((input-(int)input)*60)/1;
	printf("%d hours %dminutes %dseconds\n",hour,min,sec);

	return 0;
}
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
Math.h also has a floor function if you wanted to use standard libraries instead
 
Supporting Business
Discord Voice
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
PHP Shit Show 2k26
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
Thanks, dividing by 1 helped clear up my code a bit.

This was for a simple programming assignment, and here's the product :p

Code:
#include <stdio.h>

int main(void)
{
	float input;
	int hour, min, sec;

	printf("How many minutes did the trains take to meet?\n");
	scanf("%f",&input);

	hour = (int)input/60;
	min = ((int)input/1)%60;
	sec = ((input-(int)input)*60)/1;
	printf("%d hours %dminutes %dseconds\n",hour,min,sec);

	return 0;
}

Some tips:
Scanf is a shitty function prone to all sorts of unintended behaviour. You are much better off capturing the input into a buffer then using sscanf. In this case the input is so simple that not much can go wrong, but it's good practice all the same.

For an overflow-safe read from STDIN use fgets:
Fgets -- Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

As the manpage says, the fgets function will behave as a read function should: reading up to the number of chars specified, a new line char or EOF, which ever comes first. The function also provides a return value indicating the success of the read. If the read fails (i.e. EOF has been reached or broken pipe etc.) then it returns null.

You should also note that all scanf functions return the number of elements in the format string that they successfully scanned. So for example if you had "%f %f %f" but your input string was "50.0 5.1" then it would return 2.

So for example (untested):

Code:
char buffer[51];
if (fgets(buffer, 50, stdin)) {
    float x = 0;
    if (sscanf(buffer, "%f", &x) == 1) {
        // float was read from input do calcs here
    } else {
        fprintf(stderr, "input did not contain a float\n");
    }
} else {
    fprintf(stderr, "read from stdin failed\n");
}
 
Runescape Gamblingrunescape gambling
Back
Top