Get the App
SLTechnology News&Howtos  ›  Development  › 

How does python convert a floating-point string with a decimal to an integer

Shulou Source: shulou.com Published: 2022-06-01 21:58:38 10月12日 Update

Today, I would like to share with you how python converts floating-point strings with decimals into integers with detailed content and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

The following is perfectly acceptable in python:

1. Pass the string representation of the integer to int

2. Pass the string representation of float to float

However, if you pass a float string to int, you will get an error.

> int ('5') 5 > float ('5') 5.0 > float ('5') 5.0 > int (5) > > float (5) 5.0 > int ('5.0') Traceback (most recent call last): File ", line 1, in ValueError: invalid literal for int () with base 10:' 5.0' > > int (float ('5.0'))

Add: solve the problem of floating point conversion of python string

ValueError: invalid literal for int () with base 10

Recovery error: str_a = '1.5'int_a = int (str_a) Correction error: # Correction method 1str_a =' 1.5'int_a = int (float (str_a)) # Correction method 2str_a = '1.5'int_a = int (eval (str_a)) error cause

Floating-point strings cannot be converted directly to int types

That's all of the article "how python converts floating-point strings with decimals to integers". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Characters strings floating points knowledge articles errors integers content decimals forms ways different big reasons most that is more knowledge points types industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Huawei Shulou Information vpn