8

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

2 Answers 2

14

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

'{:,.2f}'.format(abs(value))
Sign up to request clarification or add additional context in comments.

Comments

7

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("-","")

2 Comments

Stripping only works if the number is at the start of the resulting string; if the format is 'Your number is: {:,.2f}' instead you have a problem than only abs() can solve..
abs() will not work if you have a number with decimal point like -10.0, but lstrip does

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.