Spyros Dekavallas
Spyros Dekavallas

Reputation: 1

How to format a number represented as a string?

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?

Upvotes: 0

Views: 73

Answers (3)

Synthaze
Synthaze

Reputation: 6090

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))

Upvotes: 0

Victor Ermakov
Victor Ermakov

Reputation: 491

just convert it to int and divide by 100

int(price)/100

Upvotes: 2

Sushil
Sushil

Reputation: 131

Convert the price to string.

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

Upvotes: 1

Related Questions