View Full Version : Eliminate Leading Zeros in PHP
redsox9
02-27-2006, 01:32 PM
Okay, me again: :p
I would like to be able to output numbers like batting average, slugging percentages, etc. so that they read .342 and not 0.342. Anyone have a quick fix to get rid of that zero in front? I tried using the TRIM function but I either didn't implement it correctly or it's not the proper solution. I'm having trouble finding an answer in my PHP instruction manual or online.
2notch
02-27-2006, 09:49 PM
I'm not sure how to implement it, but function "preg_replace" may be what you need.
http://us2.php.net/manual/en/function.preg-replace.php
EDIT: After a little digging...maybe this will work:
$output = $yournumber;
preg_replace ('/^0.$/', '.', $output);
echo $output[0];
$yournumber is 0.342
/^0.$/ matches the first zero and dot
'.' is the replacement in $output
try substr which lets you output only a portion of a string. If you can be certain that your numbers always begin with 0.xxx, it should work.
<?
// this assumes the number is already formatted to the appropriate number of digits
$number = 0.234;
echo substr($number,1);
// This rounds the number to three places first and then lops off the first character.
echo "<p>";
$hits = 3;
$at_bats = 9;
$batting_average = ($hits/$at_bats);
echo substr(round($batting_average,3), 1);
?>
(go sox!)
redsox9
02-28-2006, 06:12 AM
Wow, this forum is great! Thanks, 2notch and bonz... :)
redsox9
02-28-2006, 06:29 PM
try substr which lets you output only a portion of a string. If you can be certain that your numbers always begin with 0.xxx, it should work.
// This rounds the number to three places first
// and then lops off the first character.
echo "<p>";
$hits = 3;
$at_bats = 9;
$batting_average = ($hits/$at_bats);
echo substr(round($batting_average,3), 1);
(go sox!)
Oops - actually, the last line of this snippet should read:
echo substr(number_format($batting_average,3), 1);
but you successfully got the point across. Sox fans just communicate on a different level. :D
hmm, I tested mine, but that'll work too.
That's three ways, and there are probably three more.
>> Sox fans just communicate on a different level.
right on!
redsox9
02-28-2006, 06:41 PM
hmm, I tested mine, but that'll work too.
That's three ways, and there are probably three more.
I tested your code but it wasn't working for me. Then I referenced my Wrox PHP bible's list of mathematical functions and it stated that the round() function rounds the named number to the nearest integer. Eh...
I still appreciated the help! Like you said, there's problem three more ways and then some. :rolleyes:
This from the PHP site: (for academic purposes, not to make a federal case of it)
float round ( float val [, int precision] )
Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).
http://php.net/round
Oooooh, but if you have .5, you want it padded with zeros, like .500. I was focusing on getting rid of the zero at the front.
round won't add the zeros on the end, but number_format will. You got it.
for the fun of it...
print '<table border=1><tr><th></th><th>round($hits/$at_bats, 3)</th><th>number_format($hits/$at_bats,3)</th></tr>';
for ($at_bats=1; $at_bats <= 10; $at_bats++) {
for ($hits=0;$hits<=$at_bats; $hits++) {
print "<tr><td>$hits for $at_bats</td><td>";
print round($hits/$at_bats, 3);
print "</td><td>";
print number_format($hits/$at_bats, 3);
print "</td></tr>";
}
}
print "</table>";
Basil
03-01-2006, 12:44 AM
<?php
function clean($num){
$num = number_format($num,3); //format to 3 places
return strstr($num,'.'); //cut everything off in front of the decimal
}
echo clean('0007200.344900'); //.345
echo "<br>"; //line break..
echo clean(3/9); //.333
echo "<br>"; //line break..
echo clean('.5'); //.500
?>
redsox9
03-01-2006, 06:07 AM
This from the PHP site: (for academic purposes, not to make a federal case of it)
http://php.net/round
Oooooh, but if you have .5, you want it padded with zeros, like .500. I was focusing on getting rid of the zero at the front.
round won't add the zeros on the end, but number_format will. You got it.
for the fun of it...
print '<table border=1><tr><th></th><th>round($hits/$at_bats, 3)</th><th>number_format($hits/$at_bats,3)</th></tr>';
for ($at_bats=1; $at_bats <= 10; $at_bats++) {
for ($hits=0;$hits<=$at_bats; $hits++) {
print "<tr><td>$hits for $at_bats</td><td>";
print round($hits/$at_bats, 3);
print "</td><td>";
print number_format($hits/$at_bats, 3);
print "</td></tr>";
}
}
print "</table>";
*sound of 2x4 hitting me in side of head*
Ah, I get where you were going with this... okay, that makes more sense to me. No worries, mate! :D
redsox9
03-01-2006, 06:09 AM
<?php
function clean($num){
$num = number_format($num,3); //format to 3 places
return strstr($num,'.'); //cut everything off in front of the decimal
}
echo clean('0007200.344900'); //.345
echo "<br>"; //line break..
echo clean(3/9); //.333
echo "<br>"; //line break..
echo clean('.5'); //.500
?>
Hey, I like that, too!
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.