user1681664
user1681664

Reputation: 1811

matlab string number formatting

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% )

Upvotes: 0

Views: 54

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112769

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

Upvotes: 3

Related Questions