+ 1

How to convert float into integer

If we calculate the sum of two numbers and the results could be either : 16 + 16.2 = 32.2 16 + 16 = 32.0 So how do i give output like 32 if the sum is 32.0 and give output 32.2 if the sum is 32.2 (No round off)

24th Aug 2021, 2:03 AM
Sacar
Sacar - avatar
5 Answers
+ 6
Hi! Previously, I misunderstood your question. You can add this snippet after you calculated sum variable. I hope this is what you're asking if sum%1 ==0: print(int(sum)) else: print(sum)
24th Aug 2021, 2:37 AM
Python Learner
Python Learner - avatar
24th Aug 2021, 2:41 AM
Sacar
Sacar - avatar
+ 1
Sacar How about this code? :- foo = lambda f: f if f % 1 else int(f) # I hope that this helps. Happy coding!
27th Aug 2021, 6:41 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Delicate Cat and ?? Foo(sum) ??
27th Aug 2021, 6:46 AM
Sacar
Sacar - avatar
0
Sacar Yup, foo(num) returns the result.
27th Aug 2021, 6:47 AM
Calvin Thomas
Calvin Thomas - avatar