Bird
0
0

How would you use sprintf to format a floating-point number x so that it shows a plus sign for positive numbers and exactly two decimals?

hard📝 Application Q9 of 15
MATLAB - String Handling
How would you use sprintf to format a floating-point number x so that it shows a plus sign for positive numbers and exactly two decimals?
Example: +3.14, -2.00
Astr = sprintf('%+.2', x);
Bstr = sprintf('%+.2f', x);
Cstr = sprintf('%+2f', x);
Dstr = sprintf('%+.2d', x);
Step-by-Step Solution
Solution:
  1. Step 1: Understand plus sign and decimal formatting

    %+ adds sign; .2f formats float with 2 decimals.
  2. Step 2: Check options

    str = sprintf('%+.2f', x); correctly combines %+ and .2f; others are invalid or wrong specifiers.
  3. Final Answer:

    str = sprintf('%+.2f', x); -> Option B
  4. Quick Check:

    Use %+ for sign and .2f for decimals [OK]
Quick Trick: Use %+ to show sign, .2f for two decimals [OK]
Common Mistakes:
  • Using %+.2d which is integer format
  • Incorrect order of specifiers
  • Missing plus sign flag

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes