简体   繁体   English

使用round()获取精确值 - PHP

[英]Using round() to get exact value - PHP

Hey guy's I am working on a project of mine which involves the use of money. 嘿伙计我正在研究我的一个涉及使用钱的项目。 I am trying to not use round anymore because, it's rounding things to nearest tenth and I need exact numbers. 我试图不再使用圆形,因为它将四舍五入到最接近的十分之一,我需要精确的数字。 One reason I was using it, was because it was giving a whole number. 我使用它的一个原因是因为它给出了一个整数。

The way I am working my number system is that 100 = $1, 2000 = $20, etc.. I was currently using the round function because it would get rid of the decimal point for me and give me a whole number, lets say: 223 which in turn would = $2.23 . 我使用我的数字系统的方式是100 = $1, 2000 = $20, etc..我当前正在使用圆函数,因为它会为我删除小数点并给我一个整数,让我们说: 223反过来= $2.23

Here is what I am using: 这是我正在使用的:

$amount += round(($amount / 29) + 30);

Here are the numbers: 这是数字:

Lets say we have a charge of 100 and we add 125 which equal 225 (USD $2.25) . 假设我们收取100 and we add 125 which equal 225 (USD $2.25)的费用, 100 and we add 125 which equal 225 (USD $2.25) Now we add taxes and processing: + 2.9% + $.30 . 现在我们添加税收和处理: + 2.9% + $.30 After multiplying 2.25 by 2.9% and adding .30 the amount would be: 0.36525 - this is the amount that should be added than to the $2.25 which than would be 261 = $2.61 在将2.25 by 2.9%乘以2.25 by 2.9%并增加.30 ,金额将为: 0.36525 - 这是应该加到的$2.25 which than would be 261 = $2.61

The issue is because of the rounding, when I look in my Stripe panel (I am using Stripe API for payments) I see a charge of $2.63 . 问题是由于四舍五入,当我查看我的条纹面板(I am using Stripe API for payments)我看到收费$2.63 So my question is, how would I go about making it exact without having any rounding and decimal places. 所以我的问题是,如果没有任何舍入和小数位,我将如何准确地完成它。

UPDATE: 更新:

Here is the above example more explained: 以上是更多解释的上述示例:

Lets say we have a charge of 100 and we add 125 which equal 225 (USD $2.25) . 假设我们收取100 and we add 125 which equal 225 (USD $2.25)的费用, 100 and we add 125 which equal 225 (USD $2.25) Now we add taxes and processing: + 2.9% + $.30 . 现在我们添加税收和处理: + 2.9% + $.30 After multiplying 2.25 by 2.9% and adding .30 the amount would be: 0.36525 - this is the amount that should be added than to the $2.25 which than would be 261 = $2.61 在将2.25 by 2.9%乘以2.25 by 2.9%并增加.30 ,金额将为: 0.36525 - 这是应该加到的$2.25 which than would be 261 = $2.61

So now with that the actual value of amount that should be charged is $2.61 but instead when using the round it gives me 263 which also means $2.63 . 所以现在应该收取的金额的实际价值是$2.61但是当使用该轮时,它给我263 ,这也意味着$2.63 The above example is the simple math that is correct. 上面的例子是正确的简单数学。

In order to avoid calculation hiccups like that, only round the final result. 为了避免这样的计算打嗝,只绕过最终结果。 Keep all other calculations as accurate as possible: 保持所有其他计算尽可能准确:

$original = 100;
$original += 125;

$tax = $original * 2.9 / 100; //+2.9%
$tax += 30; //+$.30

$original += $tax; //Add tax.

echo $original; //Result is 261.525. Round as you please.

You can specify precision and rounding method to keep it consistent ( PHP round() ), then you can deal with the actual values. 您可以指定精度和舍入方法以保持一致( PHP round() ),然后您可以处理实际值。 Doing math tricks like multiplication by a multiple of 10 will only make it more confusing in the long run. 数学技巧如乘以10的倍数只会让它在长期内更加混乱。

$amount += round(($amount / 29) + 30, 2, PHP_ROUND_HALF_UP);

Will this solve your problem? 这会解决你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM