3
$\begingroup$

I would like to display numbers like 9.0001 and 10.0001 in the fixed-width form of xx.xxxx. This seems like a perfect job for:

NumberForm[#, {6, 4}, NumberPadding -> {"0", "0"}] & /@ {9.0001, 10.0001}

But this returns {009.0001, 010.0001}, which has one extra leading zero.

Reducing the number of digits doesn't fix this.

NumberForm[#, {5, 4}, NumberPadding -> {"0", "0"}] & /@ {9.0001, 10.0001}

{09.0001,010.0000}

Why is at least one copy of the left NumberPadding always being added, even when this makes the total number of digits exceed n, the desired total number of digits?

$\endgroup$
1
  • $\begingroup$ @MikeHoneychurch, thanks for the reminder. While your answer contains a good solution to the problem, m_goldberg's answers the exact question. $\endgroup$ Commented May 20, 2014 at 11:48

1 Answer 1

1
$\begingroup$

I think Mathematica is padding one extra position on the left to preserve space for a possible minus sign.

NumberForm[#, {6, 4}, NumberPadding -> {"*", "0"}] & /@ 
  {1234.567, -1234.567, 12.34567, -12.34567, 1.234567, -1.234567}
 {*1234.5700, -1234.5700, *12.3457, -12.3457, **1.2346, *-1.2346}
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.