如何在 PHP 中舍入小数以匹配 Google 的结果

发布于 2024-12-29 14:14:16 字数 627 浏览 1 评论 0原文

我正在尝试开发一个 php 代码来以与 Google 计算器返回结果相同的方式匹配小数,所使用的数学是 3.14159265 (pi) * 三位数,例如在 php pi * 123 中返回 386.41589595,但 Google 返回 386.415896。我尝试了ceil、round等功能,没有效果。我需要谷歌数学用来四舍五入/减少 pi * 数字的确切方法,

这是因为将用于的应用程序仅使用以谷歌计算器的方式建模的 pi * 数字结果返回准确的结果,任何帮助将不胜感激。这里有一些其他的例子,我试图弄清楚用什么数学来四舍五入,但到目前为止我很不幸。

pi * 205 = 644.026494 (谷歌结果,我想要/需要,抓取谷歌是不够的,我需要开发一个本地php方法来获得这个确切的结果,其中“205”通常是一个随机数三位数,仅在本例中用作示例)

在 php 205*pi = 644.02649325 中,我需要句点后面的数字与结果完全匹配谷歌的计算器返回。

我该如何实施呢?基本上我需要谷歌返回 pi * 数字(三位数)的确切结果,而不必依赖谷歌(抓取结果)。在 php 中一定有某种方法可以做到这一点,但到目前为止我的实验还没有结果。我不是最好的数学学生。我希望得到一些帮助,理查德。谢谢。

I am trying to develop a php code to match decimals in the same exact way which Google calculator returns results, the math being used is 3.14159265 (pi) * three digits, for example in php pi * 123 returns 386.41589595, however Google returns 386.415896. I have tried the ceil, round, and other functions to no avail. I need the exact methodology Google math uses to round/reduce its numbers for pi * digits

This is because an application this will be used for only returns accurate results using pi * digit results modelled in Google calculator's fashion, any help would be appreciated. Here are some other examples, I have tried to figure out what math is being used to round but I have been unfortunate so far.

pi * 205 = 644.026494 (Google results, which I want/need, scraping Google will not suffice, I need to develop a native php method of obtaining this exact result, where "205" is will usually be a random number three digit number, just using as example in this case)

in php 205*pi = 644.02649325, I need the numbers after the periods to match exactly the results Google's calculator returns.

How could I go about implementing this? Basically I need the exact results google would return for pi * digit (three digit number), without having to depend on Google (scraping the results). There must be some way to do this in php but the my experimentation has been fruitless thus far. I am not the best math student. I would appreciate some help, Richard. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2025-01-05 14:14:16
round(pi() * 123,6) gives 386.415896
round(pi() * 123,6) gives 386.415896
沧笙踏歌 2025-01-05 14:14:16
<?php
$test =pi() * 123;
echo $test.'<br />';
$test1 = round($test, 6, PHP_ROUND_HALF_UP);
$test2 = round($test, 6, PHP_ROUND_HALF_DOWN);
echo $test1.'<br />';
echo $test2.'<br />';

386.41589639154 < Can't really become 5 at the end
386.415896
386.415896
?>
<?php
$test =pi() * 123;
echo $test.'<br />';
$test1 = round($test, 6, PHP_ROUND_HALF_UP);
$test2 = round($test, 6, PHP_ROUND_HALF_DOWN);
echo $test1.'<br />';
echo $test2.'<br />';

386.41589639154 < Can't really become 5 at the end
386.415896
386.415896
?>
噩梦成真你也成魔 2025-01-05 14:14:16

使用此

echo bcmul('3.14159265', '123', 8),"\n";

bcmul 函数将采用第三个参数作为精度

参考这个

use this

echo bcmul('3.14159265', '123', 8),"\n";

this bcmul function will take third parameter as precision

refer this

撧情箌佬 2025-01-05 14:14:16

检查您的精度/近似设置。这应该适合你

echo round(205*M_PI,6); 

Check your precision / approximation settings. This should work for you

echo round(205*M_PI,6); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文