Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

echo mysql result with only 2 decimal places



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

5+ Year Member

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'"


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

5+ Year Member

solved eventually - i love google :)

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


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

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

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.


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

5+ Year Member

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 ;)

Featured Threads

Hot Threads This Week

Hot Threads This Month