Jump to content

Php Calculation Decimal Format


Recommended Posts

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!

Link to comment
Share on other sites

$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 by computermax2328
Link to comment
Share on other sites

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.



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.