user3789691
user3789691

Reputation: 3

python String to number

I'm trying to convert a string that contains a decimal number like this.

bet1size = "0.00000001"

I've tried

betsize = int(bet1size)

but that comes up with

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0.00000001'

I've also tried

betsize = float(bet1size)

but this comes up with 1e-08 and

betsize = Decimal(bet1size)

comes with with 1E-8

Why does it this happen and how can I do it properly?

Upvotes: 0

Views: 1138

Answers (3)

Jakob
Jakob

Reputation: 1129

The scientific notation is just a convenient way of storing/printing a floating point number. When there are a lot of leading zeros as in your example, the scientific notation might be easier to read.

In order to print a specific number of digits after a decimal point, you can specify a format string with print:

format(float(bet1size), '.8f')

Upvotes: 2

GuiTeK
GuiTeK

Reputation: 1731

int means integer. You can't store a decimal number in a int. For the two other, it means 1 * 10^-8, which equals to 0.00000001.

Upvotes: 0

unwind
unwind

Reputation: 399703

It's being done "properly" by float(). 1e-08 is "0.00000001".

It's just a matter of how you format it for printing, the number is the same.

Upvotes: 0

Related Questions