只是一个关于 str_replace 的问题
我对 PHP 中的 str_replace
有疑问。当我这样做时:
$latdir = $latrichting.$Lat;
If (preg_match("/N /", $latdir)) {
$Latcoorl = str_replace(" N ", "+",$latdir);
}
else {
$Latcoorl = str_replace ("S ", "-",$latdir);
}
print_r($latdir);
print_r($Latcoorl);
print_r($latdir);
给出 :N52.2702777778
但 print_r ($Latcoorl);
给出 :N52.2702777778000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000
是的,它添加了很多零。有人可以解释这种行为只是为了好玩吗?
print_r ($latrichting);
给出:N
print_r ($Lat);
这是一个奇怪的长数字。
所以你认为它可能不是 str_replace 命令?
I have a question about str_replace
in PHP. When I do:
$latdir = $latrichting.$Lat;
If (preg_match("/N /", $latdir)) {
$Latcoorl = str_replace(" N ", "+",$latdir);
}
else {
$Latcoorl = str_replace ("S ", "-",$latdir);
}
print_r($latdir);
print_r($Latcoorl);
print_r($latdir);
gives :N52.2702777778
but print_r ($Latcoorl);
gives :N52.270277777800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Yes, it adds a lot of zeros. Can someone explane this behavior just for the fun of it?
print_r ($latrichting);
give's: N
print_r ($Lat);
This give's the weird long number.
So its probably not the str_replace command, you think ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
新年快乐。
Happy New Year.
您的字符串替换搜索字符串在“N”之前有一个空格,而转储的值看起来像是 N:
但不确定它与所有零有什么关系。
Your string replace search string has a space before the 'N' while the dumped value looks like it's N:
Not sure what it has to do with all the zeros though.
在我的系统上,此代码片段:
给出以下结果:
我最好的猜测是,在此代码之后有一些内容打印出一系列 0。
On my system this code fragment:
gives the following result:
My best guess is you have something after this code that prints out a serie of 0.
我会怎样做;只是安东尼原始答案的一个变体,它将所有内容保持为数字并且不会陷入字符串模式。
How I would do it; just a variation of Anthony's original answer that keeps everything as numeric and doesn't lapse into string mode.
您所做的字符串操作不会生成任何 0。
0 必须来自 $lat。你用 $lat 做了什么?可以除以 pi 吗? PHP 将尝试在 $lat 中存储最准确的浮点数。这并不是真正的问题,这是正确的行为。只需在显示时截断数字或将其四舍五入即可。
The string operations you did won't generate any 0s.
The 0s have to come from $lat. What did you do with $lat? any division by pi? PHP will try to store the most accurate possible float number in $lat. That's not really a problem, its a correct behavior. Just truncate the number when displayed, or round it up.