# How to directly use formatted numbers (e.g. AccountingForm) in calculations?

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?

• This isn't just an AccountingForm[] thing, the line is repeated, for instance, in NumberForm[]. – Feyre Mar 6 '17 at 14:08
• @Feyre Yes, I am using AccountingForm as an example; hence the "e.g.". – Edmund Mar 6 '17 at 14:34
• quite recent: 139366 – Kuba Mar 6 '17 at 15:46
• 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. – george2079 Mar 6 '17 at 15:55
• @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. – Fred Simons Mar 6 '17 at 16:34

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.

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


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 *)

• A little bit simpler: take x[[1]] if you want to get rid of the head AccountingForm of x. – Fred Simons Mar 6 '17 at 16:28
• If you want to retain the wrapper, use ReplacePart[x, 1 -> x[[1]]/2] – Bob Hanlon Mar 6 '17 at 22:37