Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading