# Problem with multiple decimal points (Python)

## Question:

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.

If I understood your problem correctly, the split function would be enough as a solution if used like this:

``````data = ["-0.2179", "-8.742.754.508", "1.698.516.678"]
fund = []
for number in data:
split = number.split('.')
integer_part = split[0]
fractional_part = ''.join([split[i] for i in range(1, len(split))])
new_number = float('.'.join((integer_part, fractional_part)))
# rounding to two decimal points
new_number = round(new_number, 2)
fund.append(new_number)

print(fund)
``````

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
``````
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.