Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a way to convert some values of numbers into currency (dollars). The example I display below takes in three random numbers between -4 and 4, and rounds them to the second decimal place. How could I create the output in $$ though? Should I try something rule based?

Clear[sequence];
sequence = RandomReal[{-4, 4}, 3];
Map[(N[Round[10^2 *#]/(10^2)]) &, sequence]

{-3.73, 2.03, 0.31}

But I want -$3.73 and so on...

share|improve this question
cash = N[Round[10^2*#]/(10^2)] & /@ RandomReal[{-4, 4}, 3]

{-3.21, 2.64, -2.6}

If[Sign[#] > 0, "$" <> ToString[#], 
   "-$" <> ToString[Abs[#]]] & /@ cash

{"-\$3.21", "\$2.64", "-\$2.6"}

Note the output is a list of strings.


Or

q = Quantity[cash, "USDollars"]

{\$-3.21, \$2.64, \$-2.6}

The output is a list of Quantities:

QuantityQ /@ q

{True, True, True}

share|improve this answer

Quantity and Units are your friends. One way could be:

Clear[sequence];
sequence = RandomReal[{-4, 4}, 3];
d = Map[(N[Round[10^2*#]/(10^2)]) &, sequence]

{3.82,3.64,0.}

Quantity[d, "USD"]

{$ 3.82,$ 3.64,$ 0.}

share|improve this answer
Map[$, N[Round[10^2*#]/(10^2)] & /@ RandomReal[{-4, 4}, 3]]
    (*{$[3.61], $[2.45], $[-1.79]}*)

Is that what you want?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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