Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 If a decimal is 0 how do I make it not show?
#1
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?
Quote
#2
You probably just want to call int() on the number.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#3
(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?
Quote
#4
Ah, ok, yeah that's a little different. You can try using the modulo operator (e.g. your_num % 1 == 0).
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#5
(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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  testing for Decimal w/o importing decimal every time Skaperen 7 587 May-06-2019, 10:23 PM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)