Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report