Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove negative sign in string format in python

i'd like to know if it is possible to remove the negative sign from '{:,.2f}'.format(number) only using format.

So that

'{:,.2f}'.format(10) ## 10

'{:,.2f}'.format(-10) ## 10

Thanks in advance

like image 900
jaxkodex Avatar asked Aug 28 '14 22:08

jaxkodex


2 Answers

You can't with str.format() or format() alone. Use abs() on the number instead:

'{:,.2f}'.format(abs(value))
like image 200
Martijn Pieters Avatar answered Oct 15 '22 02:10

Martijn Pieters


Use abs

 '{:,.2f}'.format(abs(-10))

Or lstrip:

num = -10
print '{:,.2f}'.format(num).lstrip("-")
10.00

Or:

num = -10
print 'Your number is: {:,.2f}'.format(num).replace("-","")
like image 23
Padraic Cunningham Avatar answered Oct 15 '22 02:10

Padraic Cunningham