0

how can I convert this type of number: 2.000000000000000000e+00 to an int (valued 2)?

Thanks!

def read_labels_from_file(file_name):
    labels_file = open(file_name, "r")
    labels = []

    for line in labels_file:
        line_sepetated = line.split("\n")
        labels.append(int(line_sepetated[0]))
    return labels

I expected to have append the integer 2 to labels, but got: ValueError: invalid literal for int() with base 10: '2.000000000000000000e+00'

gusfring
  • 35
  • 6
  • Possible duplicate of [Converting number in scientific notation to int](https://stackoverflow.com/questions/32861429/converting-number-in-scientific-notation-to-int) – Isma May 16 '19 at 19:50

1 Answers1

0

2.000000000000000000e+00 is a valid floating point literal, so you can convert it to a float first before converting it to an int:

labels.append(int(float(line_sepetated[0])))
blhsing
  • 91,368
  • 6
  • 71
  • 106