问题
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