Bird
0
0

You want to store prices with exact decimal precision in PHP to avoid float errors. Which approach is best?

hard📝 Application Q8 of 15
PHP - Variables and Data Types
You want to store prices with exact decimal precision in PHP to avoid float errors. Which approach is best?
AUse floats and round when displaying
BUse integers to store cents (e.g., 199 for $1.99)
CStore prices as strings and convert when needed
DUse boolean type for prices
Step-by-Step Solution
Solution:
  1. Step 1: Understand float precision issues with money

    Floats can cause rounding errors, so storing money as floats is risky.
  2. Step 2: Identify best practice for money storage

    Using integers to store smallest units (like cents) avoids float errors.
  3. Final Answer:

    Use integers to store cents (e.g., 199 for $1.99) -> Option B
  4. Quick Check:

    Integer cents for money = B [OK]
Quick Trick: Store money as integer cents to avoid float errors [OK]
Common Mistakes:
  • Using floats directly for money
  • Storing money as strings without conversion
  • Using boolean for numeric values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes