Float to Int: What Gets Lost?
Beginner · Write the code · from Python Type Conversion: How to Convert Between Data Types
From the lesson: Python Type Conversion: How to Convert Between Data Types
Imagine you ask someone their age and they write it on a sticky note. You now have the text "25" - but you can't do math with a sticky note. You need to read the text and turn it into an actual number in your head first.
Read the lessonYou have a float price = 9.99. Convert it to an integer and print both the original and converted values to show what gets lost.
Expected output:
Original: 9.99
Converted: 9
Lost: 0.99Hint: calculate what was lost by subtracting. Use round() on the lost amount to avoid floating-point weirdness.
price = 9.99
# Convert to int
# Calculate what was lost
# Print all three values
More Type Conversion exercises
- String to Int and MultiplyBeginner
- Predict the Output: Type ConversionsBeginner
- Fix the Bug: String and Int ConcatenationBeginner
- Safe Number ConverterBeginner
All Python exercises · 50 practice problems · Python Fundamentals quiz