Hi. I have an issue who i can't solve.

The problem is that a = "13.33" and I want it to turn a = 13.33 and not 13.3333333333 and not 13.330000002.

So, how do i make it so "13.33" will be an 13.33 float?

heavy.is.happy -2 Newbie Poster

Hi. I have an issue who i can't solve.

The problem is that a = "13.33" and I want it to turn a = 13.33 and not 13.3333333333 and not 13.330000002.

So, how do i make it so "13.33" will be an 13.33 float?

TrustyTony 888 pyMod Team Colleague Featured Poster

I do not see problem for that:

```
>>> a='13.33'
>>> a=float(a)
>>> a
13.33
>>> print(a-13.33)
0.0
>>>
```

heavy.is.happy -2 Newbie Poster

I do not see problem for that:

`>>> a='13.33' >>> a=float(a) >>> a 13.33 >>> print(a-13.33) 0.0 >>>`

But test this.

>>> new = "18,33"

>>> a = new.replace(",",".")

>>> a

'18.33'

>>> a = float(a)

>>> a

18.329999999999998

>>>

It must be 18.33.

TrustyTony 888 pyMod Team Colleague Featured Poster

It is:

```
Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> new = '18.33'
>>> new = '18,33'
>>> a = new.replace(',', '.')
>>> a
'18.33'
>>> a = float(a)
>>> a
18.33
>>>
```

```
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> new = '18,33'
>>> a = new.replace(',', '.')
>>> a
'18.33'
>>> a = float(a)
>>> a
18.33
>>>
```

Gribouillis 1,391 Programming Explorer Team Colleague

But test this.

>>> new = "18,33"

>>> a = new.replace(",",".")

>>> a

'18.33'

>>> a = float(a)

>>> a

18.329999999999998

>>>It must be 18.33.

It's impossible to have a float exactly equal to the mathematical 18.33 because 18.33 has too many (or an infinity of) binary digits when it is represented in base 2. Read this http://docs.python.org/tutorial/floatingpoint.html?highlight=arithmetics .

TrustyTony 888 pyMod Team Colleague Featured Poster

There some interesting builtin methods for floating point numbers by the way:

```
>>> a=18.33
>>> a.as_integer_ratio()
(1289859080776581L, 70368744177664L)
>>> a.hex()
'0x1.2547ae147ae14p+4'
>>>
```

heavy.is.happy -2 Newbie Poster

It is:

`Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> new = '18.33' >>> new = '18,33' >>> a = new.replace(',', '.') >>> a '18.33' >>> a = float(a) >>> a 18.33 >>>`

`Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> new = '18,33' >>> a = new.replace(',', '.') >>> a '18.33' >>> a = float(a) >>> a 18.33 >>>`

>>> new = '18,33'

>>> a = new.replace(',','.')

>>> a

'18.33'

>>> a = float(a)

>>> a

18.329999999999998

>>>

: S

Gribouillis 1,391 Programming Explorer Team Colleague

>>> new = '18,33'

>>> a = new.replace(',','.')

>>> a

'18.33'

>>> a = float(a)

>>> a

18.329999999999998

>>>: S

Also, read this thread to learn about the machine representation of floating point numbers http://www.daniweb.com/software-development/python/threads/349945/1486914#post1486914

heavy.is.happy -2 Newbie Poster

Is it's impossible?

Gribouillis 1,391 Programming Explorer Team Colleague

Is it's impossible?

Since the floating point representation uses 64 bits, it can only represent 2 ** 64, about 18 billion billion numbers. Since there is an uncountable infinity of real numbers, almost all of them must be approximated. So, yes, for most numbers, it's impossible. Also notice that for many problems, this precision suffices.

Edited by Gribouillis because: * n/a *

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.