Convert a string to integer with decimal in Python
问题 I have a string in the format: 'nn.nnnnn' in Python, and I'd like to convert it to an integer. Direct conversion fails: >>> s = '23.45678' >>> i = int(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '23.45678' I can convert it to a decimal by using: >>> from decimal import * >>> d = Decimal(s) >>> print d 23.45678 I could also split on '.', then subtract the decimal from zero, then add that to the whole number ...