Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove .0
#1
Hey everyone. I've written this code. It all works fine except the answer always has a .0 . The answer is correct, but is there a way to get rid of the .0? Thanks

number_1 = 1 
number_2 = 1 
 
enter = int(input('Please enter first number: ')) 
 
value = enter 
 
while value > 0: 
 
    number_1 = number_1 * value 
 
    value = value - 1 
 
print('Intermediate result 1:', number_1) 
 
 
enter_2 = int(input('Please enter second number: '))     
 
sub = enter - enter_2 
 
value = sub 
 
while value > 0: 
 
    number_2 = number_2 * value 
 
    value = value - 1 
 
      
print('Intermediate result 2:', number_2) 
 
   
print('The Answer is:', number_1 / number_2) 
Output:
The Answer is: 120.0
Reply
#2
You can convert the result to integer data type, int(number_1 / number_2). Also, you can just use integer division, e.g. number_1 // number_ 2.
Reply
#3
Hello scidam. Thanks for the quick reply. Both are cool. Would you recommend to do this though or is it just a matter of preference?
Reply
#4
I suggest to learn concept of datatypes.

There are built in numeric datatypes: int, float and complex. So using int() to 'remove .0' is not removing at all but converting.

Beware of this expected behaviour while converting:

>>> int(5.9)  # for floating point numbers truncates towards zero; this is not rounding
5


// (floor division) behaviour:

>>> 5 // 2
2
>>> 4 // 2
2
So if there is any chance that you may end up with not integer value as a result of division you will not get correct result with both int() and //.

...and I don't want to open Pandora's box of floating point representation precision in binary
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020