2
$\begingroup$

The documentation for AccountingForm states:

AccountingForm acts as a "wrapper", which affects printing, but not evaluation.

However,

x = AccountingForm[1500.12, {7, 2}, DigitBlock -> 3];
x/2
1,500.12 / 2

Where 1,500.12 is not resolved to its number and 750.06 is not returned as either a number nor an AccountingForm number. What does the documentation statement mean? It appears that the formatting functions do affect evaluation as no evaluation occurs if a formatted number is used. Bug or am I missing something?

$\endgroup$
  • $\begingroup$ This isn't just an AccountingForm[] thing, the line is repeated, for instance, in NumberForm[]. $\endgroup$ – Feyre Mar 6 '17 at 14:08
  • $\begingroup$ @Feyre Yes, I am using AccountingForm as an example; hence the "e.g.". $\endgroup$ – Edmund Mar 6 '17 at 14:34
  • 1
    $\begingroup$ quite recent: 139366 $\endgroup$ – Kuba Mar 6 '17 at 15:46
  • 1
    $\begingroup$ yes its an annoying deficiency in the documentation. Just think how much grief they would save newcomers (and us) with a few words to the effect that the results of "*Form" should not be used for further processing. $\endgroup$ – george2079 Mar 6 '17 at 15:55
  • $\begingroup$ @george2079. That is not quite right. As long as you do not assign the Form expression to a symbol, you can further process the result. If by abuse you assigned the Form expression to a symbol, you can use the result by taking part 1. $\endgroup$ – Fred Simons Mar 6 '17 at 16:34
1
$\begingroup$

AccountingForm and similar formatters are typeset with an InterpretationBox:

InterpretationBox[boxes, expr] is a low-level box construct that displays as boxes but is interpreted on input as expr.

The internal "kernel" FullForm is still AccountingForm[1500.12,...], but the box form in the front end is

Cell[BoxData[
 TagBox[
  InterpretationBox["\<\"1,500.12\"\>",
   1500.12,
   AutoDelete->True],
  AccountingForm[#, {7, 2}, DigitBlock -> 3]& ]], "Output",
 CellChangeTimes->{3.697810013552223*^9}]

When the expression is copied in the front end and pasted as input, the InterpretationBox is passed to the kernel and "interpreted," that is, the second argument 1500.12 is used for the value.

The OP's example, with the output copied as input:

One can do the same thing programmatically with

ToExpression@ToBoxes[x/2, StandardForm]
(*  750.06  *)

The upshot is that output-formatters are intended to be used for formatting output, not internally in computations. At least, that seems to be the intention.

Appendix

I just noticed this subtlety:

If the output of the first line is typeset, then in the subsequent %/2, the AccountingForm is interpreted as a number; if the output suppressed with a semicolon, then the internal FullForm is used and AccountingForm[..] is left unchanged.

$\endgroup$
0
$\begingroup$

My guess is that they mean the evaluation of the arguments to AccountingForm.

x = AccountingForm[1500.12, {7, 2}, DigitBlock -> 3]

For sure you can not use x directly in a computation. This is clear if you look at the FullForm.

x/2 // FullForm

Mathematica graphics

Instead attempt to extract the numerical value:

(x /. AccountingForm[z_?NumericQ, y___] -> z)/2

(* 750.06 *)

Fed Simons commented that an easier way is to use Part.

x[[1]]/2

(* 750.06 *)
$\endgroup$
  • $\begingroup$ A little bit simpler: take x[[1]] if you want to get rid of the head AccountingForm of x. $\endgroup$ – Fred Simons Mar 6 '17 at 16:28
  • $\begingroup$ @FredSimons Added your suggestion to the answer. $\endgroup$ – Jack LaVigne Mar 6 '17 at 16:43
  • $\begingroup$ If you want to retain the wrapper, use ReplacePart[x, 1 -> x[[1]]/2] $\endgroup$ – Bob Hanlon Mar 6 '17 at 22:37

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.