0

I want have a number

a = 0.0123

and I want to convert it to a string in XX.XX% format. How can I do this? The best I've got to is:

sprintf('%f%%',a*100)

this gets me

1.23000000%

How do I specify I want 2 numbers in the front of decimal and 2 in the back (i.e. 01.23% or if it was 0.123, then 12.30% )

1 Answer 1

3

Use this:

sprintf('%05.2f%%', a*100)

The meaning is:

  • 0: left-pad with zeros if needed
  • 5: width 5 in total (integer part, decimal dot and decimal part)
  • .2: two decimals
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Greatly appreciated

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.