Trimming Excess Digits After Calculations

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ut1205
Forum Commoner
Posts: 32
Joined: Thu Jan 29, 2004 5:12 pm

Trimming Excess Digits After Calculations

Post by ut1205 »

I have a MYSQL Database which holds "Miles Per Gallon, etc" info on my vehicles.

I do queries to determine total fuel purchased, total miles driven, etc. and do division computations and post the result to a table.

When I do something like:

$AverMPG = $TotalMiles / $GalPurchased

Then print $AverMPG to a cell in my table I get a result like:
16.584752

Where and how do I trim it down to one or two numbers after the decimel.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

ut1205
Forum Commoner
Posts: 32
Joined: Thu Jan 29, 2004 5:12 pm

Post by ut1205 »

I sincerely appreciate your response but I have already been there and didn't understand it. I thought that I might a simple answer or someone who could explain it in "NewBee" terms.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

Use one of these functions to shorten your number. For example with round, just add this after the average calculation:

Code: Select all

<?php
$AverMPG = round($AverMPG,2); //keeps only 2 digits 
?>
ut1205
Forum Commoner
Posts: 32
Joined: Thu Jan 29, 2004 5:12 pm

Post by ut1205 »

:D Thanks, That Works
Post Reply