📖 Scenario: You are working on a simple billing system that needs to handle prices with decimal points accurately.
🎯 Goal: Learn how to create float numbers, set precision, and display float values correctly in PHP.
📋 What You'll Learn
Create float variables with exact values
Set a precision variable for decimal places
Format float numbers using the precision
Print the formatted float values
💡 Why This Matters
🌍 Real World
Handling prices and money values in billing or shopping cart systems requires careful control of decimal places to avoid errors.
💼 Career
Many programming jobs require working with float numbers and formatting them correctly for display or calculations, especially in finance and e-commerce.
Progress0 / 4 steps
1
Create float variables
Create two float variables called price1 and price2 with the exact values 10.456 and 20.789 respectively.
PHP
Hint
Use the $ sign to create variables and assign decimal numbers directly.
2
Set precision variable
Create an integer variable called precision and set it to 2 to represent two decimal places.
PHP
Hint
Use a simple integer variable to store the number of decimal places you want.
3
Format float numbers using precision
Create two new variables called formattedPrice1 and formattedPrice2. Use the number_format function with price1, price2, and precision to format the prices to two decimal places.
PHP
Hint
The number_format function formats a float to a string with the specified number of decimals.
4
Print formatted float values
Print the values of formattedPrice1 and formattedPrice2 each on a new line using echo.
PHP
Hint
Use echo and add a newline character \n to print each value on its own line.
Practice
(1/5)
1. What is the main reason floats in PHP can sometimes give unexpected results when compared directly?
easy
A. Because PHP does not support decimal numbers
B. Because floats are stored as strings internally
C. Because floats have limited precision and tiny rounding errors can occur
D. Because floats always round down to the nearest integer
Solution
Step 1: Understand float storage
Floats are stored in a way that cannot represent all decimal numbers exactly, causing small rounding errors.
Step 2: Impact on comparisons
These tiny errors mean direct equality checks (==) can fail unexpectedly.
Final Answer:
Because floats have limited precision and tiny rounding errors can occur -> Option C
Quick Check:
Float precision limits cause rounding errors [OK]
Hint: Remember floats can't represent all decimals exactly [OK]
Common Mistakes:
Thinking floats are stored as exact decimals
Assuming PHP treats floats as strings
Believing floats always round down
2. Which of the following is the correct way to round a float to 2 decimal places in PHP?
easy
A. round(3.14159)
B. round(3.14159, '1')
C. round(3.14159, 0.02)
D. round(3.14159, 2)
Solution
Step 1: Check round() function usage
The round() function takes a float and an integer number of decimal places as arguments.
Step 2: Validate each option
round(3.14159, 2) uses round(3.14159, 2) which rounds to 2 decimals correctly. round(3.14159, '1') uses a string '1' which is invalid. round(3.14159) rounds to 0 decimals by default. round(3.14159, 0.02) uses 0.02 which is not a valid precision argument.