String manipulations

Discussion in 'Python' started by Lorn, May 28, 2005.

  1. Lorn

    Lorn Guest

    I'm trying to work on a dataset that has it's primary numbers saved as
    floats in string format. I'd like to work with them as integers with an
    implied decimal to the hundredth. The problem is that the current
    precision is variable. For instance, some numbers have 4 decimal places
    while others have 2, etc. (10.7435 vs 1074.35)... all numbers are of
    fixed length.

    I have some ideas of how to do this, but I'm wondering if there's a
    better way. My current way is to brute force search where the decimal
    is by slicing and then cutoff the extraneous numbers, however, it would
    be nice to stay away from a bunch of if then's.

    Does anyone have any ideas on how to do this more efficiently?

    Many Thanks,
    Lorn
     
    Lorn, May 28, 2005
    #1
    1. Advertising

  2. Multiply them by 10000 ?

    Lorn wrote:

    > I'm trying to work on a dataset that has it's primary numbers saved as
    > floats in string format. I'd like to work with them as integers with an
    > implied decimal to the hundredth. The problem is that the current
    > precision is variable. For instance, some numbers have 4 decimal places
    > while others have 2, etc. (10.7435 vs 1074.35)... all numbers are of
    > fixed length.
    >
    > I have some ideas of how to do this, but I'm wondering if there's a
    > better way. My current way is to brute force search where the decimal
    > is by slicing and then cutoff the extraneous numbers, however, it would
    > be nice to stay away from a bunch of if then's.
    >
    > Does anyone have any ideas on how to do this more efficiently?
    >
    > Many Thanks,
    > Lorn
     
    Philippe C. Martin, May 28, 2005
    #2
    1. Advertising

  3. Lorn

    Lorn Guest

    Yes, that would get rid of the decimals... but it wouldn't get rid of
    the extraneous precision. Unfortunately, the precision out to the ten
    thousandth is noise... I don't need to round it either as the numbers
    are artifacts of an integer to float conversion. Basically, I need to
    know how many decimal places there are and then make the necessary
    deletions before I can normalize by adding zeros, multiplying, etc.

    Thanks for your suggestion, though.
     
    Lorn, May 28, 2005
    #3
  4. Lorn

    John Roth Guest

    "Lorn" <> wrote in message
    news:...
    > I'm trying to work on a dataset that has its primary numbers saved as
    > floats in string format. I'd like to work with them as integers with an
    > implied decimal to the hundredth. The problem is that the current
    > precision is variable. For instance, some numbers have 4 decimal places
    > while others have 2, etc. (10.7435 vs 1074.35)... all numbers are of
    > fixed length.


    > I have some ideas of how to do this, but I'm wondering if there's a
    > better way. My current way is to brute force search where the decimal
    > is by slicing and then cutoff the extraneous numbers, however, it would
    > be nice to stay away from a bunch of if then's.
    >
    > Does anyone have any ideas on how to do this more efficiently?


    If you can live with a small possibility of error, then:

    int(float(numIn) * 100.0)

    should do the trick.

    If you can't, and the numbers are guaranteed to have a decimal point,
    this (untested) could do what you want:

    aList = numIn.split(".")
    result int(aList[0]) * 100 + int(aList[1][:2])

    HTH

    John Roth
    >
    > Many Thanks,
    > Lorn
    >
     
    John Roth, May 28, 2005
    #4
  5. On May 28, 2005, at 2:52 PM, Lorn wrote:

    > Yes, that would get rid of the decimals... but it wouldn't get rid of
    > the extraneous precision. Unfortunately, the precision out to the ten
    > thousandth is noise... I don't need to round it either as the numbers
    > are artifacts of an integer to float conversion. Basically, I need to
    > know how many decimal places there are and then make the necessary
    > deletions before I can normalize by adding zeros, multiplying, etc.
    >
    > Thanks for your suggestion, though.


    for s in numbers:
    decimal_index = s.find('.')
    decimal_places = len(s) - decimal_index - 1

    Anything wrong with this? (it will mess up if there isn't a decimal
    but you can fix that if you want)

    -- Elliot Temple
    http://www.curi.us/


    ---
    [This E-mail scanned for viruses by Declude Virus]
     
    Elliot Temple, May 28, 2005
    #5
  6. Lorn

    Lorn Guest

    Thank you Elliot, this solution is the one I was trying to come up
    with. Thank you for your help and thank you to everyone for their
    suggestions.

    Best regards,
    Lorn
     
    Lorn, May 29, 2005
    #6
    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. Stan Sainte-Rose

    Image Manipulations (how to set legends)

    Stan Sainte-Rose, Oct 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    567
    jongalloway
    Oct 2, 2004
  2. PyPK

    Pixel Manipulations

    PyPK, Sep 28, 2005, in forum: Python
    Replies:
    1
    Views:
    331
    Terry Reedy
    Sep 28, 2005
  3. PyPK

    Pixel Manipulations

    PyPK, Sep 28, 2005, in forum: C++
    Replies:
    6
    Views:
    341
    mlimber
    Sep 29, 2005
  4. Lawrence D'Oliveiro

    Pathname Manipulations

    Lawrence D'Oliveiro, Mar 4, 2011, in forum: Java
    Replies:
    4
    Views:
    364
    Roedy Green
    Mar 4, 2011
  5. string manipulations

    , Oct 26, 2007, in forum: ASP General
    Replies:
    5
    Views:
    144
    Dooza
    Nov 20, 2007
Loading...

Share This Page