Python Forum
Converting float (2.0) to integer(2)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting float (2.0) to integer(2)
#3
The float has the method is_integer().
You can use it.

By the way, if you expect different results with rounding, it's
scientific rounding: Half to even.

lst = [2, 3.96343, 5.635, 2.32463255, 7.0, 12, 1.0]
k=[]
for i in lst:
    if isinstance(i, int):
        k.append(i)
    elif isinstance(i, float):
        if i.is_integer():
            k.append(int(i))
        else:
            k.append(round(i,2))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Converting float (2.0) to integer(2) - by Raj_Kumar - Dec-07-2019, 10:12 AM
RE: Converting float (2.0) to integer(2) - by DeaD_EyE - Dec-07-2019, 11:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  float object can't be interpreted as an integer t23 2 1,085 Jan-18-2025, 07:36 PM
Last Post: deanhystad
  python calculate float plus float is incorrect? sirocawa 6 2,363 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  converting user input to float troubles RecklessTechGuy 3 3,786 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  How to check if user entered string or integer or float?? prateek3 5 13,863 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 4,820 Jul-25-2019, 11:32 PM
Last Post: scidam
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 17,068 Dec-15-2017, 02:01 AM
Last Post: micseydel
  Storing float as integer in a database Ageir 6 6,927 Aug-17-2017, 08:03 PM
Last Post: Ageir

Forum Jump:

User Panel Messages

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