str to float (rounded)

Discussion in 'Python' started by Nader, Jun 10, 2008.

  1. Nader

    Nader Guest

    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
    Nader, Jun 10, 2008
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Jun 10, 2008
    #2
    1. Advertising

  3. Nader

    Nader Guest

    On Jun 10, 4:30 pm, "Diez B. Roggisch" <> 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.
    Nader, Jun 10, 2008
    #3
  4. >
    > 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
    Diez B. Roggisch, Jun 10, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    476
    Thomas G. Marshall
    Aug 3, 2003
  2. bd
    Replies:
    0
    Views:
    621
  3. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    629
    CBFalconer
    Apr 10, 2004
  4. sushant

    float values can b rounded off?

    sushant, Jan 21, 2005, in forum: C Programming
    Replies:
    13
    Views:
    483
    Jonathan Burd
    Jan 27, 2005
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,536
    James Kanze
    Oct 8, 2009
Loading...

Share This Page