computermax2328 Posted September 27, 2012 Share Posted September 27, 2012 Hello Everyone, Haven't been here in a while. This looks new and cool! Anyway, I have PHP output an array of numbers as an average and that worked fine. I got the number -.0111523. Which is exactly what I want. They only problem is that I need the number to look like this -.011. I tried to use the number format command, but that did not work. Just slapped a couple of zeros onto the end. Any suggestions? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted September 27, 2012 Share Posted September 27, 2012 ...what didn't work about it? Number_format is exactly what you want. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 27, 2012 Share Posted September 27, 2012 Code.... Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2012 Share Posted September 27, 2012 Formatting a float less than 1 is going to give a zero before the decimal point so you will need to remove it yourself. Something like <?php $n = -.0111523; $nstr = number_format($n, 3); if (abs($n < 1)) { $nstr = str_replace('0.', '.', $nstr); } echo $nstr; ?> Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted September 27, 2012 Author Share Posted September 27, 2012 (edited) $avg = "SELECT AVG(era) FROM $table"; $avgquery = mysql_query($avg, $connection); while($avgrow = mysql_fetch_array($avgquery)) { $avgera = $avgrow['AVG(era)']; } echo number_format(decimals($avgera), 3, ".", ","); Gives me -.0111523.00 decimals is a function that uses str_replace to strip the zero from the front of the decimal number. Edited September 27, 2012 by computermax2328 Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 27, 2012 Share Posted September 27, 2012 Sounds like decimals() is returning a string, complete with the -., rather than an actual float. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2012 Share Posted September 27, 2012 I can't imagine what it produces. I tried passing a string and all it did was reinsert the zero before the decimal <?php $n = "-.0111523"; echo number_format($n,3); //--> -0.011 ?> Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 27, 2012 Share Posted September 27, 2012 Reverse the order of the two function calls (number_format () and decimals ()), and it should work better. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted September 27, 2012 Share Posted September 27, 2012 number_format does what you want, but it puts a 0 before the decimal. If you wish to remove it, you should wrap your decimals() function AROUND the number_format function. Functions in programming languages are called inside-out, the innermost ones are resolved first. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2012 Share Posted September 27, 2012 I guess you do have to spell it out. I already demonstrated removing the "0" after using number_format Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted September 27, 2012 Author Share Posted September 27, 2012 Good stuff! Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.