PHP Remove Decimal Numbers??

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
6,013
Reaction score
1,199
Location
Canada
Hi everyone,

I have started getting my hands down and dirty with PHP, and I am kind of stuck with this one problem, which I really think is small, but has been annoying me for the past 20 minutes now. Basically, I decided to write out this small code where the user enters a number (in minutes) and then PHP will process and output the number in hours and minutes. EG: 62 Minutes is 1 hour and 2 minutes. (I have put up a testing version here if you want to see what I mean: http://testingsite.koeksworld.com/hms.php )

Here is the deal though, the code works, for all above 60 minutes. When the user enters a number under 60 minutes, lets say 54, it will return, 54 minutes is 1 hour and 54 minutes...this isn't correct obviously. I do know what I need to do here is to somehow trim away the decimal part of the number.... I tried number_format, and format() and int() and those haven't worked yet.

I been having so much fun with PHP then this shows up....can someone please give me a hint if not the answer??

Thanks a lot :)
 
Hi everyone,

I have started getting my hands down and dirty with PHP, and I am kind of stuck with this one problem, which I really think is small, but has been annoying me for the past 20 minutes now. Basically, I decided to write out this small code where the user enters a number (in minutes) and then PHP will process and output the number in hours and minutes. EG: 62 Minutes is 1 hour and 2 minutes. (I have put up a testing version here if you want to see what I mean: http://testingsite.koeksworld.com/hms.php )

Here is the deal though, the code works, for all above 60 minutes. When the user enters a number under 60 minutes, lets say 54, it will return, 54 minutes is 1 hour and 54 minutes...this isn't correct obviously. I do know what I need to do here is to somehow trim away the decimal part of the number.... I tried number_format, and format() and int() and those haven't worked yet.

I been having so much fun with PHP then this shows up....can someone please give me a hint if not the answer??

Thanks a lot :)

Show your code. It's a logic error somewhere when you mod probably.
 
If entered number < 60 return entered number else do division thingy that works?


It's not a "good" solution but you are coding it in PHP so I assume you don't really cate anyway :p :twisted:
 
Hi everyone,

I have started getting my hands down and dirty with PHP, and I am kind of stuck with this one problem, which I really think is small, but has been annoying me for the past 20 minutes now. Basically, I decided to write out this small code where the user enters a number (in minutes) and then PHP will process and output the number in hours and minutes. EG: 62 Minutes is 1 hour and 2 minutes. (I have put up a testing version here if you want to see what I mean: http://testingsite.koeksworld.com/hms.php )

Here is the deal though, the code works, for all above 60 minutes. When the user enters a number under 60 minutes, lets say 54, it will return, 54 minutes is 1 hour and 54 minutes...this isn't correct obviously. I do know what I need to do here is to somehow trim away the decimal part of the number.... I tried number_format, and format() and int() and those haven't worked yet.

I been having so much fun with PHP then this shows up....can someone please give me a hint if not the answer??

Thanks a lot :)

Need an idea of your code. All I can see is if its 29 mins and below it works well. If its 30 mins and above it adds the extra hour. Which means it is rounding up instead of down for the hour
 
I tried if (enteredNumber < 60) then totalHours = 0; and that did not work (this was all in code obviously)

Here is the code I had written for this:

PHP:
<?php

$totalHours = 0;
$totalMinutes = 0;

$totalTime = $_GET['totalMinutes'];

$totalHours = $totalTime / 60;
$totalHours = number_format($totalHours, 0);

$totalMinutes = $totalTime % 60;

if ($totalTime != NULL)
{
	echo ('Entered value <strong>'.$totalTime.'</strong> is '.$totalHours.' hours and '.$totalMinutes.' minutes');
}

else
{
	echo "Please enter a value";
}

?>
 
If entered number < 60 return entered number else do division thingy that works?

If statements waste processing power. Should be possible using mod only.

Code:
input = any number  of minutes you'd like
mins = input mod 60
hours = (input - mins) / 60
 
I tried if (enteredNumber < 60) then totalHours = 0; and that did not work (this was all in code obviously)

Here is the code I had written for this:

PHP:
<?php

$totalHours = 0;
$totalMinutes = 0;

$totalTime = $_GET['totalMinutes'];

$totalHours = $totalTime / 60;
$totalHours = number_format($totalHours, 0);

$totalMinutes = $totalTime % 60;

if ($totalTime != NULL)
{
	echo ('Entered value <strong>'.$totalTime.'</strong> is '.$totalHours.' hours and '.$totalMinutes.' minutes');
}

else
{
	echo "Please enter a value";
}

?>

Hours are rounded up if your input >= 30 minutes. Use my code above. Much cleaner.
 
If you must do it your way, this should also work:

PHP:
<?php

$totalHours = 0;
$totalMinutes = 0;

$totalTime = $_GET['totalMinutes'];

$totalHours = $totalTime / 60;
$totalHours = floor($totalHours);

$totalMinutes = $totalTime % 60;

if ($totalTime != NULL)
{
    echo ('Entered value <strong>'.$totalTime.'</strong> is '.$totalHours.' hours and '.$totalMinutes.' minutes');
}

else
{
    echo "Please enter a value";
}

?>

Edit: note "floor"
 
Last edited:
The only thing that made my skin crawl was

PHP:
$totalHours = number_format($totalHours, 0);

Quickly checked and yes, yes it does round floating point input to conform to the desired number of decimal places.

If you really want to use number_format then use floor before using number_format:

PHP:
$totalHours = number_format(floor($totalHours));
 
Thanks a lot, everyone. I really really appreciate the help I have gotten here. I'm sorry to have put my small issue here, but I have learnt from this thread.

Funny enough the floor() function did the trick....I was pretty sure I tried it, ow well my brain must have been real slow at the time. As for Baron, thanks for the short way of doing it - I think after doing a number of exercises I will redo them, this time making sure code is efficient.

Thanks for the links imranpanji, will be going through them today....wow so much info on a small issue :)

The road to PHP just got started :)
 
As for Baron, thanks for the short way of doing it. I think after doing a number of exercises I will redo them, this time making sure code is efficient.

Code should be efficient from the start. Never substitute the computing power available today for good quality code.

Sitting down with a pen and paper before coding is one of the best things you can do to solve a problem like this (or any logic/math problem you need to solve with code).
 
If statements waste processing power. Should be possible using mod only.

If you're using a 486 as a server perhaps, and there is barely any branch prediction penalty in his if statement. So there is not going to be any slow down in cpu cycles/processing power.
 
If you're using a 486 as a server perhaps, and there is barely any branch prediction penalty in his if statement. So there is not going to be any slow down in cpu cycles/processing power.

This is exactly why the quality of code that programmers produce is so poor. No-one thinks about the code they're churning out, it's all about quantity rather than quality. If you can't code the small bits efficiently, how will you manage when faced with a big problem?

Why would you want to put an if-statement in koeks525's code when I've provided a solution that does not require it? Why even advocate a solution that is not only less optimal, but also the longer way of solving it? Decision-making wastes time (and a remarkable amount of time, at that). Right now you're converting one value at a time. What if you had to do it for a million values in a DB?

Do things right from the start. Here, read an introduction on why efficient code matters today, even with all our computing power and memory available: http://hashcookie.net/uploads/2010e445bf_algorithms.xps (see the comparison of running times between the 2 computers).
 
This is exactly why the quality of code that programmers produce is so poor. No-one thinks about the code they're churning out, it's all about quantity rather than quality. If you can't code the small bits efficiently, how will you manage when faced with a big problem?

Why would you want to put an if-statement in koeks525's code when I've provided a solution that does not require it? Why even advocate a solution that is not only less optimal, but also the longer way of solving it? Decision-making wastes time (and a remarkable amount of time, at that). Right now you're converting one value at a time. What if you had to do it for a million values in a DB?

Do things right from the start. Here, read an introduction on why efficient code matters today, even with all our computing power and memory available: http://hashcookie.net/uploads/2010e445bf_algorithms.xps (see the comparison of running times between the 2 computers).

Got our wires crossed, i was not referring to the same if-statement you were. In your case yes the equation would make sense.

My bad.
 
Top
Sign up to the MyBroadband newsletter
X