How to truncate decimal value without loosing precision

偶尔善良 提交于 2019-12-25 21:24:09

问题


I have current variable php:

$latlng = "<a href='http://www.google.com/maps/place/".$lat.",".$lng."/@".$lat.",".$lng.",".$zoom."' target='_blank'>Lat ".$lat." Lng ".$lng."</a>";

echo output is as following example:

i want truncate it so at end i want have following result:

I have tried following code but it cut only final part:

$cut_string = substr($latlng,0,strpos($latlng,'</a>')-12);
echo "$cut_string <br/>\r\n";

Thanks


回答1:


I think I understand your problem.
You want the link with full precision but a easy to read link text?

This should be what you need:

$latlng = "<a href='http://www.google.com/maps/place/".$lat.",".$lng."/@".$lat.",".$lng.",".$zoom."' target='_blank'>Lat ".round($lat,3)." Lng ".round($lng,3)."</a>";

As you can see the link to google is intact but the text is rounded to three decimals.




回答2:


You could use round, but you will loose precision. If that works, then

echo round(39.959082, 3);

But, using substr, you could maintain the precision.

$lat = "39.959082";
echo substr($lat,0,strrpos($lat,'.')+4);

will display

39.959


来源:https://stackoverflow.com/questions/46503823/how-to-truncate-decimal-value-without-loosing-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!