0
$\begingroup$

I am trying to print a number with padding using the function NumberForm in order to get the number format like "01.1234567" where there are 9 significant digits with 7 to the right of the decimal point. This is meant to be similar to the seconds part of a time specification such as "01:15:01.123467". I would have thought this function would achieve the result but I seem to get an extra padded "0" to the left:

ToString@NumberForm[1.1234567, {9, 7}, NumberPadding -> {"0", "0"}]

but instead it returns:

001.1234567

Does anyone have a suggestion as to how I can get the result "01.1234567" using this function or is this a bug and I need to trying an alternative? Many thanks.

$\endgroup$
2
$\begingroup$

The extra "0" is added by the default NumberSigns -> {"-",""} option. You can remove it by setting NumberSigns -> {"",""}, but then negative numbers will be displayed without the minus sign:

NumberForm[-1.1234567, {9, 7}, NumberPadding -> {"0", "0"}]
NumberForm[1.1234567, {9, 7}, NumberPadding -> {"0", "0"}]
NumberForm[-1.1234567, {9, 7}, NumberPadding -> {"0", "0"}, NumberSigns -> {"", ""}]

0-1.1234567

001.1234567

01.1234567

This aspect of behavior of NumberForm isn't documented AFAIK.

$\endgroup$
  • $\begingroup$ Many thanks!!!! $\endgroup$ – rfrasier Mar 23 '17 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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