What if your program could handle money calculations perfectly without you worrying about decimals?
Why Integer and Float number types in Ruby? - Purpose & Use Cases
Imagine you are trying to calculate the total cost of items in a shopping cart by adding prices manually on paper, mixing whole numbers and decimals.
Doing math by hand with whole numbers and decimals is slow and easy to mess up, especially when you need exact answers or many calculations.
Using Integer and Float number types in Ruby lets the computer handle whole numbers and decimals correctly and quickly, avoiding mistakes and saving time.
total = 10 + 5.75 + 3 + 2.5 # Manually adding mixed numbers
total = 10 + 5.75 + 3 + 2.5 # Ruby uses Integer and Float types automatically
This lets you do fast and accurate math with both whole numbers and decimals in your programs.
Calculating a restaurant bill where some items cost whole dollars and others have cents, and you want the exact total.
Integer type stores whole numbers without decimals.
Float type stores numbers with decimals.
Ruby uses these types to do math correctly and efficiently.