user469652
user469652

Reputation: 51241

Python regular expression: decimal part of float

How can I accomplish the following transformations with a regular expression in Python?

0.44 -> 44
0.7867 -> 78
1.00 -> 100

Upvotes: 0

Views: 141

Answers (3)

Charles Beattie
Charles Beattie

Reputation: 5949

How about *100 and convert to int

t = lambda x: int (x*100)
t(0.44)

Upvotes: 0

Andrew Jaffe
Andrew Jaffe

Reputation: 27087

def twodigitint(x):
   return int(100*float(x))

(or use round instead of int if you really want 0.7867->79)

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838176

Parse the input as a floating point number then multiply it by 100 and truncate to an integer:

result = int(float(s) * 100)

Upvotes: 5

Related Questions