如何在不丢失精度的情况下截断十进制值
How to truncate decimal value without loosing precision
我有当前变量php:
$latlng = "<a href='http://www.google.com/maps/place/".$lat.",".$lng."/@".$lat.",".$lng.",".$zoom."' target='_blank'>Lat ".$lat." Lng ".$lng."</a>";
回显输出如下例:
我想截断它,所以最后我想得到以下结果:
我尝试了以下代码,但它只删除了最后一部分:
$cut_string = substr($latlng,0,strpos($latlng,'</a>')-12);
echo "$cut_string <br/>\r\n";
谢谢
您可以使用 round
,但会降低精度。如果可行,那么
echo round(39.959082, 3);
但是,使用 substr
,您可以保持精度。
$lat = "39.959082";
echo substr($lat,0,strrpos($lat,'.')+4);
会显示
39.959
我想我明白你的问题了。
您想要 link 完全精确但易于阅读的 link 文本?
这应该是您需要的:
$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>";
如您所见,link 到 google 是完整的,但文本四舍五入到小数点后三位。
我有当前变量php:
$latlng = "<a href='http://www.google.com/maps/place/".$lat.",".$lng."/@".$lat.",".$lng.",".$zoom."' target='_blank'>Lat ".$lat." Lng ".$lng."</a>";
回显输出如下例:
我想截断它,所以最后我想得到以下结果:
我尝试了以下代码,但它只删除了最后一部分:
$cut_string = substr($latlng,0,strpos($latlng,'</a>')-12);
echo "$cut_string <br/>\r\n";
谢谢
您可以使用 round
,但会降低精度。如果可行,那么
echo round(39.959082, 3);
但是,使用 substr
,您可以保持精度。
$lat = "39.959082";
echo substr($lat,0,strrpos($lat,'.')+4);
会显示
39.959
我想我明白你的问题了。
您想要 link 完全精确但易于阅读的 link 文本?
这应该是您需要的:
$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>";
如您所见,link 到 google 是完整的,但文本四舍五入到小数点后三位。