Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > str to float (rounded)

Reply
Thread Tools

str to float (rounded)

 
 
Nader
Guest
Posts: n/a
 
      06-10-2008
Hello,

I have a list of tuple with strin elements. These elements are number,
but they are save as string. Now I will change the string to number
which will be rounded. An example will make it more clear.

t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]

And I will have the next list:

t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]

The elements of tuple are not more as string.

Would somebody tell me how I can do that?

Regards,
Nader

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      06-10-2008
Nader wrote:

> Hello,
>
> I have a list of tuple with strin elements. These elements are number,
> but they are save as string. Now I will change the string to number
> which will be rounded. An example will make it more clear.
>
> t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]
>
> And I will have the next list:
>
> t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]
>
> The elements of tuple are not more as string.
>
> Would somebody tell me how I can do that?


use

float("123.45")

to convert a string to a float.

Of course you need to do that on all your elements above by e.g. a
list-comprehension.

Diez
 
Reply With Quote
 
 
 
 
Nader
Guest
Posts: n/a
 
      06-10-2008
On Jun 10, 4:30 pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> Nader wrote:
> > Hello,

>
> > I have a list of tuple with strin elements. These elements are number,
> > but they are save as string. Now I will change the string to number
> > which will be rounded. An example will make it more clear.

>
> > t = [('35.757', '-0.239'), ('33.332', '-2.707'), ('33.640', '-2.423')]

>
> > And I will have the next list:

>
> > t = [(35.76, -2.24), (33.33, -2.71), (33.64, -2.42)]

>
> > The elements of tuple are not more as string.

>
> > Would somebody tell me how I can do that?

>
> use
>
> float("123.45")
>
> to convert a string to a float.
>
> Of course you need to do that on all your elements above by e.g. a
> list-comprehension.
>
> Diez


If I do the next :

t1 = [(round(float(x),1), round(float(y),2)) for x, y in t]

I get the long float as :

[(35.799999999999997, -0.23999999999999999), (33.299999999999997,
-2.71), (33.600000000000001,-2.4199999999999999)]

But I would have a float with 2 decimal numbers.


 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      06-10-2008
>
> If I do the next :
>
> t1 = [(round(float(x),1), round(float(y),2)) for x, y in t]
>
> I get the long float as :
>
> [(35.799999999999997, -0.23999999999999999), (33.299999999999997,
> -2.71), (33.600000000000001,-2.4199999999999999)]
>
> But I would have a float with 2 decimal numbers.


There is no such thing as a float with only two decimal numbers. This has
been discussed on this ML a bazillion times - what you see above are
rounding errors due to the approximation of decimal values by binary
floating points.

You can *convert a float to a string* and specify a precision when printing:

>>> print "%.2f" % 2.4199999999999999999999999

2.42

Or you can use the module decimal to work with numbers base 10 - which you
can also limit to certain precisions. See a recent thread on this ML,
google for "Alternative to decimal type"


Diez


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
It is fun.the result of str.lower(str()) Sullivan WxPyQtKinter Python 5 03-09-2006 08:09 AM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM
what's the deference between str=null and str=" " ???????? David Java 2 08-03-2003 04:10 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments