0

I have a text file that has a price column like 000245 and like 001245. I want to format the strings as 2.45 and 12.45?

3 Answers 3

2

just convert it to int and divide by 100

int(price)/100 
Sign up to request clarification or add additional context in comments.

1 Comment

btw There is no casting in python. Your example is of a conversion.
1

Convert the price to string.

>>> price = "000245" >>> int(price)/100 2.45 

Comments

0

If you work with strings and want to get a string back:

s = '000245' s = s.lstrip('0') print (s[:-2]+'.'+s[-2:]) 

Output (as string):

2.45 

Otherwise, still to preserve the variable type:

print (str(int(s)/100)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.