5
$\begingroup$

Bug introduced in 10.4 or earlier and persisting through 11.1.1

CASE:3918306 confirmed


I freshly installed Wolfram Mathematica. I can't quite remember how to output a dynamically updated variable number $x$ in dollars?

I did improvise the following:

Manipulate[AccountingForm[x, {Infinity, 2}, DigitBlock -> {3, 2}, NumberSigns -> {"-$", "+$"}], {x, -100000000,100000000, .01}](*Shows tooltipped error: "AccountingForm: Value for option DigitBlock should be a positive integer, Infinity or a pair of positive integers."*)

Alternatively, I tried:

AccountingForm[Manipulate[x, {x, -100000000, 100000000, .01}], {Infinity, 2}, DigitBlock -> {3, 2}, NumberSigns -> {"-$","+$"}](*Problem: A number like "67.2" is not displayed as the expected "+$67.20."*)

The corresponding output for each piece of code:

enter image description here

enter image description here

What's the code to output a dynamic variable number in dollars? For example, to show the numbers ${-6543.567, 556788.456789}$ as ${-$6,543.57, +$556,788.46}$.


Problem: Dynamically updating AccountingForm[].

(*Does not work*) Manipulate[AccountingForm[x, {Infinity, 2}, DigitBlock -> {3, 2},NumberSigns -> {"-$", "+$"}], {x, -100000000, 1000000000, .01}]

Solution: Define AccountingForm[] as a new identical function (with the same desired parameters)

(*Does work*) Dollars[x_] := AccountingForm[x, {Infinity, 2}, DigitBlock -> {3, 2},NumberSigns -> {"-$", "+$"}]; Manipulate[Dollars[x], {x, -100000000, 1000000000, .01}]
$\endgroup$
0

3 Answers 3

5
$\begingroup$

A workaround: Define a function that is identical to AccountingForm (with the options specified) and use it as the first argument in Manipulate

accountingForm = AccountingForm[#, {Infinity, 6}, DigitBlock -> {3, 2}, 
    NumberSigns -> {"-$", "+$"}] &;

Manipulate[accountingForm[x], {x, -10000, 10000, .0001}]

enter image description here

Also works in @Kuba's example:

Dynamic@accountingForm[-1234567890.123456]

-$1,234,567,890.12 34 56

Same approach works for other number forms. For example, for PaddedForm (which, unlike AccountingForm, uses only the first number in DigitBlock -> {3,2}) we can do

ClearAll[paddedForm]
paddedForm = PaddedForm[#, {12, 6}, ExponentFunction -> (Null &), 
    DigitBlock -> {3, 2}, NumberSigns -> {"-$", "+$"}] &;

Dynamic@paddedForm[-1234567890.123456]

-$1,234,567,890.12000000

$\endgroup$
0
3
$\begingroup$

Here's a workaround:

Manipulate[
PaddedForm[x, {12, 2},
  ExponentFunction -> (Null &),
  DigitBlock -> 3,
  NumberSigns -> {"-$", "+$"}],
 {x, -100000000, 100000000, .01}]

enter image description here

The problem occurs if you dynamically update DigitBlock -> {x, y} (also with other number forms).

$\endgroup$
4
  • 1
    $\begingroup$ If one needs to use DigitBlock -> 3 instead of {3,2} then there is no need to change anything more, is there? $\endgroup$
    – Kuba
    Commented Jul 17, 2017 at 12:02
  • $\begingroup$ If you don't use this workaround .40 would display as .4 $\endgroup$
    – eldo
    Commented Jul 17, 2017 at 12:11
  • $\begingroup$ But this is what AccountingForm does anyway: AccountingForm[.4, {Infinity, 2}, DigitBlock -> {3, 2}, NumberSigns -> {"-$", "+$"}] so if this is a problem it is another one. But right, makes sense to use it. $\endgroup$
    – Kuba
    Commented Jul 17, 2017 at 12:17
  • $\begingroup$ @eldo So with DigitBlock -> 3, .40 displays as .40 and with DigitBlock -> {3,2} it displays as .4? $\endgroup$
    – useranonis
    Commented Jul 17, 2017 at 13:15
1
$\begingroup$

Here's a simpler version that works directly:

Manipulate[
 AccountingForm[N[x], {Infinity, 2}, DigitBlock -> 3, 
  NumberSigns -> {"-$", "+$"}, 
  NumberPadding -> {"", "0"}], {x, -100000000, 100000000, .01}]

Notice that unlike most other versions presented here, it correctly displays the .00 for the initial value:

In[89]:= Manipulate[AccountingForm[N[x],{Infinity,2},DigitBlock->3, 
      NumberSigns->{"-$","+$"},NumberPadding->{"", "0"}],
      {x,-100000000,100000000,.01}]//Setting
Out[89]//AccountingForm= -$100,000,000.00

Using DigitBlock->{3,2} is useless if you're only ever going to display 2 digits based the decimal point.

$\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.