Converting float to int
examples/basics/converting_float_to_int.py
a = 2.1 print( type(a) ) # <class 'float'> print(a) # 2.1 b = int(2.1) print( type(b) ) # <class 'int'> print(b) # 2
examples/basics/converting_floating_string_to_int.py
a = "2.1" print(a) # 2.1 print( type(a) ) # <class 'str'> b = int(a) print(b) print( type(b) ) # Traceback (most recent call last): # File "converting_floating_string_to_int.py", line 5, in <module> # b = int(a) # ValueError: invalid literal for int() with base 10: '2.1'
examples/basics/converting_to_int_via_float.py
a = "2.1" b = float(a) c = int(b) print(c) # 2 print( type(a) ) # <class 'str'> print( type(b) ) # <class 'float'> print( type(c) ) # <class 'int'> d = int( float(a) ) print(d) # 2 print( type(d) ) # <class 'int'> print( int( float(2.1) )) # 2 print( int( float("2") )) # 2 print( int( float(2) )) # 2