

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
- Related Questions & Answers
- Round float and double numbers in Java
- How to convert Float array list to float array in Java?
- How to round down to 2 decimals a float using Python?
- How to convert float to integer in Python?
- PHP round() Function
- How to round MySQL column with float values and display the result in a new column?
- round() function in PHP
- How do you round up a float number in Python?
- Java Program to convert float to String
- PHP program to compare float values
- Convert from float to String in Java
- Convert from String to float in Java
- Convert tuple to float value in Python
- Convert string to integer/ float in Arduino
- How to correctly get a value from a JSON PHP?
Advertisements