Re: String manipulations



"Lorn" <efoda5446@xxxxxxxxx> wrote in message news:1117316427.826830.189260@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: dyalog
    ... APL will automatically "blow up" the data type to complex if required ... 64-bit product is that it is not clear where they fit in the sequence, ... as they have more precision than floats (IEEE double precision floats ...
    (comp.lang.apl)
  • Re: dyalog
    ... 64-bit product is that it is not clear where they fit in the sequence, ... as they have more precision than floats (IEEE double precision floats ...
    (comp.lang.apl)
  • Re: decimal precision
    ... I know that floats are approximated but I did not ... allowed in single precision floats... ... is a small number that fits easily in 15 significant digits? ... and to explicitely ask for displaying no more than this precision, ...
    (comp.lang.scheme)
  • Re: Huge Floating Point Error
    ... because the last part '1563' starts from the 8th digit of precision. ... floats are simply too small for what you want to do. ... These things are implied by the floating point standard itself. ... that VC6 yield other results is because it does floating point stuff ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Q: Precision in SQL server 2000
    ... May want to make sure you really want a float to store whatever you are ... in scientific notation and thus dictates the precision and storage ... I have a table which has several fields holding floats. ... is taking too long to download the entire table. ...
    (microsoft.public.sqlserver)