Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change float to string without decimal places

Tags:

python

If I have f=123.12, what is easier way to change to string s="123"? For now I do: s = "%.0f" % (f,).

like image 867
user3654650 Avatar asked Dec 04 '22 06:12

user3654650


1 Answers

If you want to round the value, do:

str(int(f+0.5))

If you want to truncate the value, do:

str(int(f))
like image 105
sshashank124 Avatar answered Dec 06 '22 18:12

sshashank124