homepage Welcome to WebmasterWorld Guest from 54.204.127.59
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
echo mysql result with only 2 decimal places
adeibiza




msg:4258380
 3:26 pm on Jan 26, 2011 (gmt 0)

i have a select query which basically gets the number of results that are less than a set distance and then outputs that with a simple echo:

echo 'distance=' . $row['distance'] . ' km';

the value of distance is currently 14 decimal places which is way over the top obviouslys i only need 2 decimal places

do i need to reduce decimal places in the select query or can i do it within echo ? if so how? :)

select query:

"SELECT *, ( *snip haversine formula*) AS distance FROM tablename HAVING distance < '%s'"

 

adeibiza




msg:4258436
 4:49 pm on Jan 26, 2011 (gmt 0)

solved eventually - i love google :)

echo 'distance=' . number_format($row['distance'],2) . ' km';

rocknbil




msg:4258466
 5:20 pm on Jan 26, 2011 (gmt 0)

If you're not too far into it, you might consider changing the field type to get a handle on it and gain more accuracy. A decimal field would make all this go away.

alter table change fieldname fieldname decimal(12,2) default '0.00';

Alternatively you can use the modify command or drop the column and re-add it, either way.

adeibiza




msg:4258477
 5:32 pm on Jan 26, 2011 (gmt 0)

the distance isnt actually a field - its the sum thats created by comparing current lat & lng and the lat & long of a selected row and working out the distance and only selecting rows that are less than a specified distance away

its only to show the user how far a certain place is so it doesnt have to be accurate - it was originally accurate to 14 decimal places - ie probably nanometres ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved