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.
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).
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
  Dialup Modem using pyserial - Need it to show response from the other end. Rython 1 254 Jun-20-2019, 10:09 PM
Last Post: roy
  assigned variable fails to show up Skaperen 4 145 May-27-2019, 10:48 AM
Last Post: Skaperen
  testing for Decimal w/o importing decimal every time Skaperen 7 444 May-06-2019, 10:23 PM
Last Post: Skaperen
  str.format rounding to the left of the decimal ClassicalSoul 2 208 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Force calculation result as decimal vercetty92 4 238 Mar-20-2019, 02:27 PM
Last Post: vercetty92
  decimal point or comma Skaperen 8 405 Mar-17-2019, 06:02 AM
Last Post: DeaD_EyE
  mixed decimal point characters Skaperen 1 179 Mar-10-2019, 05:23 PM
Last Post: stullis
  Using Decimal Type instead of Float Bytales 1 357 Mar-02-2019, 06:28 PM
Last Post: Bytales
  Trouble with decimal precision Cassie 4 249 Feb-14-2019, 08:23 PM
Last Post: Cassie
  Popen does not show part of the data Arkantr 6 376 Feb-01-2019, 07:09 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)