Problem with multiple decimal points (Python)

I have having a bit of a problem:

I am trying to convert these numbers:

-0.2179,
-8.742.754.508,
1.698.516.678,

to

-0.22,
-8.74,
1.70,

But I am really not sure how I do this, when the number of decimal points is different?

I have tried .split(‘.’) but its difficult with changing decimal points.

I was wondering if you guys had any pointers for this small problem? Kind regard.

for number in data.fundreturn:
    
        new_number = number.split('.')[0]
        fund.append(new_number)
   
    
for number in data.bitcoinreturn:
    
        new_number = number.split('.')[0]
        bitcoin.append(new_number)
        

but then I get 0, 8, and 1

The code snippet basically is me going through each column and trying to covert the values.

>Solution :

This solution takes into account the decimal part after the first dot and ignores the rest of the string.

def convert(number: str) -> float:
    n = number.split(".")
    return float(n[0]) if len(n)<2 else float(f"{n[0]}.{n[1]}")

convert("32")  # 32.0
convert("-8.742.754.508")  # -8.742

Leave a Reply