Python Forum

Full Version: If a decimal is 0 how do I make it not show?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new and only 30 minutes into Python. I'm trying to calculate something using randomly generated numbers and when I want to round it to 1 decimal I use
number2 = round(number1, 1)
But sometimes I'll get the output 1.0, 2.0, 3.0 etc
How do I make these numbers output to just 1, 2 or 3?
You probably just want to call int() on the number.
(Feb-08-2019, 12:25 AM)micseydel Wrote: [ -> ]You probably just want to call int() on the number.

I only want them to be an integer when they have the decimal "0". How do I do that?
Ah, ok, yeah that's a little different. You can try using the modulo operator (e.g. your_num % 1 == 0).
(Feb-08-2019, 12:57 AM)micseydel Wrote: [ -> ]Ah, ok, yeah that's a little different. You can try using the modulo operator (e.g. your_num % 1 == 0).

Ok, thanks.