PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How may I Limit decimal to two

 August 10, 2022     calculation, decimal, paypal, php     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing