Issue
Hello everyone and I hope you can help me.
I have 'value' => $amount_to_pay
and I want $amount_to_pay to me multiplied by 0.11 but the output to be limited to only 2 decimals. It is for PayPal.
I used 'value' => $amount_to_pay*0.11
but what comes after the decimal is long and getting an error back from PayPal exp: 589.24*0.11 = 64.8164 I want output to be 64.81
Thank you
Solution
You can use round()
to round at N decimals
'value' => round($amount_to_pay * 0.11, 2) // 64.82
As @PrestonPHX mentionned, that give a round value. If you want to "truncate decimals you could use :
'value' => (int)($amount_to_pay * 100) / 100 // 64.81
To use a dynamic number of decimals, you could use :
$nb = 2; // number of decimals
$exp = pow(10, $nb);
echo (int)($amount_to_pay * $exp) / $exp;
Answered By - Syscall Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.