简体   繁体   English

将十进制数从 str 转换为 float

[英]Converting a decimal number from str to float

I have the following number of type float:我有以下类型的浮点数:

1.019e-05

To convert it to a decimal number, i did this:要将其转换为十进制数,我这样做了:

print("%.10f" % float(1.019e-05))

Which gives the following output: 0.0000101900这给出了以下 output: 0.0000101900

The problem is that 0.0000101900 is of type str , but i need to make some calculations with that number.问题是0.0000101900str类型,但我需要用这个数字进行一些计算。 How can i convert 0.0000101900 from string to a number again?如何再次将0.0000101900从字符串转换为数字? if i use float() the decimal will be converted again to an exponential.如果我使用float()小数将再次转换为指数。 Am i forced to use the exponential number here?我是否被迫在这里使用指数?

The number is a float and has the same value no matter how it looks - the scientific notation used is just a way of representing how the data looks in a more concise way.这个数字是一个float ,无论它看起来如何都具有相同的值 - 使用的科学记数法只是以更简洁的方式表示数据外观的一种方式。 By doing your formatting string, you are formatting the number to look a certain way for output, but that creates a string representation of the number rather than "converting it to a decimal number".通过执行格式化字符串,您可以将数字格式化为output的某种方式,但这会创建数字的字符串表示形式,而不是“将其转换为十进制数”。 You can't change the way Python natively represents a float , but I'm not understanding why this matters to you because it doesn't change the actual value of the number, just how it looks when printed out您无法更改 Python 本机表示float的方式,但我不明白为什么这对您很重要,因为它不会改变数字的实际值,只是打印出来时的样子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM