PHP str_replace 和/或 number_format 失败
$BRL_PRICE = currency("USD", "BRL", $allmoney);
echo "BRL1 = " . $BRL_PRICE."<br />";
$BRL_PRICE = str_replace(" ", "", $BRL_PRICE);
echo "BRL2 = " . $BRL_PRICE."<br />";
$BRL_PRICE = number_format($BRL_PRICE, 2);
echo "BRL3 = " . $BRL_PRICE."<br />";
输出是..
BRL1 = 1 531.70922
BRL2 = 1 531.70922
BRL3 = 1.00
我真正想要的是将值四舍五入到最接近的美分..10位。我知道 number_format 失败,因为currency() google Money Converter 添加了一个空格而不是 comra 或空白。
我想
BRL3 = 1531.71
。
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return $var;
}
$BRL_PRICE = currency("USD", "BRL", $allmoney);
echo "BRL1 = " . $BRL_PRICE."<br />";
$BRL_PRICE = str_replace(" ", "", $BRL_PRICE);
echo "BRL2 = " . $BRL_PRICE."<br />";
$BRL_PRICE = number_format($BRL_PRICE, 2);
echo "BRL3 = " . $BRL_PRICE."<br />";
outputs are..
BRL1 = 1 531.70922
BRL2 = 1 531.70922
BRL3 = 1.00
All I really want is to round up the value to the nearest cents.. 10s place. I know number_format failures because currency() google money converter adds a space instead of a comra or blank.
I want
BRL3 = 1531.71
.
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return $var;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您懒得阅读文档,请不要责怪软件:
Don't blame software in cases when you was just too lazy to read documentation:
为什么不使用 sprintf('%.2f',$var)
Why not use
sprintf('%.2f',$var)
我不认为你的号码中有一个简单的空格。尝试
preg_replace
看看会发生什么:I don't think it is a simple space in your number. Try
preg_replace
and see what happens:您从 google 获得的响应是无效的 json。你甚至不需要
json_decode
它,而只是为了日志。str_replace
工作得很好(一如既往),您只需替换正确的字符串:即 Unicode 字符“NO-BREAK SPACE”(U+00A0)。
The response you get from google is invalid json. You don't even
json_decode
it, but just for the log.str_replace
works pretty well (as always), you just need to replace the right string:That is the Unicode Character 'NO-BREAK SPACE' (U+00A0).