I'd like to be able to copy and paste, into MMA, numbers that have a standard USD currency format, i.e., that include dollar signs and comma separators, and get the output in the same format. I'd also like this to be done automatically, without the need for a formula, which I assume means making use of one or more $Pre-type statements.
For instance, I'd like to be able to paste the following into an input cell:
$3,342.56 + $1000 + 100.5 + $45.35*1.57
and get:
$4,514.26
[The following is an edit from earlier version of this post, which used $Pre.] I can create a formula that provides the output in currency format, except that it doesn't pad to two decimals if the input number ends in the tenths place:
$PrePrint =
AccountingForm[N[#, 30], {Infinity, 2}, DigitBlock -> 3,
NumberSigns -> {"-$", "$"}] &;
4514.26
358.3
$4,514.26
$358.3
But, on the input side, I haven't figured out how to create a function that can strip dollar signs and commas from an expression. I can only get it to work if I first manually convert the expression to a string by wrapping it in quotes:
$Pre = ToExpression@StringReplace[ToString[#], {"," -> "", "$" -> ""}] &;
$PrePrint =
AccountingForm[N[#, 30], {Infinity, 2}, DigitBlock -> 3,
NumberSigns -> {"-$", "$"}] &;
"$3,342.56 + $1000 + 100.5 + $45.35*1.57"
% + 1000
%% + "$1,000"
$4,514.26
$5,514.26
$5,514.26
In fact, if I have to wrap the input in quotes anyways, I can accomplish the above using only $PrePrint; this gives the same output as the above code:
$PrePrint =
AccountingForm[
N[ToExpression@StringReplace[ToString@#, {"," -> "", "$" -> ""}],
30], {Infinity, 2}, DigitBlock -> 3,
NumberSigns -> {"-$", "$"}] &;
An explanation of what's motivating this
I get emails in which we're going back and forth discussing funding for grant proposals, and I'll need to run some calculations and reply. To do this, I'll cut and paste the numbers in the email into Mathematica, manually strip out the dollar signs and commas, do the calculation, paste the result back into my reply email, and add the dollars and commas back. Since sometimes there are many of these, it would be a nice convenience if I could avoid the two manual steps, i.e., avoid having to strip out the dollar signs and commas before doing the calculation, and having to add them back after pasting the result in the email.
Ultimately, when I enter $3,342.56 + $1000 + 100.5 + $45.35*1.57, I'd like to combine the answer to this with other code I already have such that MMA outputs:
$3,342.56 + $1000 + 100.5 + $45.35*1.57 = $4,514.26.
Then I can just paste the whole thing back into my email.


$Pre? Then you can strip the $ and the commas with your initial simple function. – Nicholas G May 09 '21 at 12:48$Prework. Even$Pre=ToString /@ {#} &, which might have worked by making the input into a list and mappingToStringover all its elements does not work. It converts$2in input alone into a list with one element but the comma that follows it makes Ma expect a list and since the list is not in braces, Ma thinks it is incomplete. – Nicholas G May 09 '21 at 21:13$Prelike that (so that it is impossible to revert) seems like a terrible idea. I'm struggling to see why this is better than just defining some unit symbol like$USDso that you can do like100 $USDand then you simply useUpValueson$USDto convert100 $USDintoQuantity[100, "Dollars"]or something. The latter seems safer, easier, and less buggy – b3m2a1 May 09 '21 at 22:18$Pre = StringJoin[ToString["$"], ToString[AccountingForm[#, DigitBlock -> 3]]] &;(I've updated the first part of my question accordingly). – theorist May 09 '21 at 22:19$Pre=.it returns to normal behavior. However, when I do this, it returns $Null--what problem does that indicate? – theorist May 09 '21 at 22:21$Prewith pretty much any type of$PreReadyou'll only ever get$$Failed– b3m2a1 May 09 '21 at 22:22$PreRead+$PrePrintcode, which can be found right after the "Update" heading:https://mathematica.stackexchange.com/questions/138800/how-do-i-alter-this-preread-preprint-statement-so-it-can-be-selectively-de?rq=1
– theorist May 09 '21 at 22:36