问题描述
我正在开发地图,我必须将此参数保存在Parse数据库中:
I'm developing a map and I have to save this parameters on Parse database:
纬度:20.6350.长:-103.5334
lat.: 20.6350 . Long: -103.5334
问题是当我将它们转换为数字时,该函数将20.6350转换为20.635. ¿我该怎么做才能使tu保留最后一个零?
The problem is when i convert them into numbers, that function converts 20.6350 to 20.635. ¿What can I do in order tu preserve the last zero?
推荐答案
您应该指定信号图,它们将正确显示.
You should specify the sig figs and they will display properly.
$lat = "20.6350";
$lng = "-103.5334";
print number_format($lat, 4); // 4 sig figs
print number_format($lng, 4);
有关其他格式设置信息,请参见 http://php.net/number_format
See http://php.net/number_format for additional formatting info
更新
根据您上面的评论,看来您要提取的字符串的长度有所不同,对吗?
Based on your comments above, seems the string you're pulling varies in length, correct?
为什么不仅要获取小数点后的信号无花果数量,还要将其用作number_format()
中的第二个参数?我敢肯定有一种更合适的处理方式,但是我相信这会起作用.
Why not just get the amount of sig figs after the decimal then use that as the second argument in number_format()
? I'm sure there is a more appropriate way to handle, but this would work I believe.
// Calculate sig figs
$length = strlen($lng) - (stripos($lng, '.') + 1);
number_format($lng, $length);
或一行
number_format($lng, strlen($lng) - (stripos($lng, '.') + 1));
这篇关于将字符串转换为数字而不会丢失小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!