- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert amount to float and round correctly in PHP?
To round amounts, use round() in PHP. Let’s say the following are our input values −
$amount=50.78; $quantity=45.5;
Convert them to float like this
$am=(float) round($amount, 4); $quant=(float) round($quantity, 4);
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $amount=50.78; $quantity=45.5; $am=(float) round($amount, 4); echo $am,"<br>"; $quant=(float) round($quantity, 4); echo $quant,"<br>"; $correctAmount=round($am*$quant,4); echo "The result is=",$correctAmount; ?> </body> </html>
Output
This will produce the following output
50.78 45.5 The result is=2310.49
Advertisements