简体   繁体   中英

How to convert very small numbers from strings to floats properly in Python?

I have a number like this:

n = "0.000016495822225857"

But after converting this to a float using:

num = float(n)

I get:

1.64958e+13

My number is less than 1, how can I convert it properly so that I can round these values that are less than 1? But right now after conversion, it's greater than 1.

Is this because of floating point precision?

No clue why this is happening (sorry about that).

What I do know is that you can use the decimal module as a workaround.

from decimal import Decimal
n = "0.000016495822225857"
d = Decimal(n)

output

0.000016495822225857

If I'm not blind, the input and output look the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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