Re: Number formatting for financial applications
- To: mathgroup at smc.vnet.net
- Subject: [mg27495] Re: [mg27475] Number formatting for financial applications
- From: Tomas Garza <tgarza01 at prodigy.net.mx>
- Date: Thu, 1 Mar 2001 03:53:21 -0500 (EST)
- References: <200102270537.AAA16262@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I see your problem. I don't think you'll find an easy way out. You see, even though Mathematica stores your number safely in order to use it appropriately whenever it is required, it doesn't easily yield to displaying it the way you want. Now, perhaps you could use one of the following approaches. In any case, you'll have to use a function which you define only for the purpose of displaying: 1) The most elementary approach. For example, In[1]:= p1[x_] := NumberForm[x, {20, 7}, ExponentFunction -> (Null &)]; Then In[2]:= p1[101522688.1906242] Out[2]//NumberForm= "101522688.1906242" Each time you need to see your result the way you want it you will have to wrap it with p1. But this is much easier than having to use all the NumberForms, NumberPaddings, etc. over and over again. 2) Of course, you may embellish it a little: In[3]:= p2[x_] := NumberForm[x, {20, 9}, NumberPadding -> {"", "0"}, DigitBlock -> 3, ExponentFunction -> (Null &)]; In[4]:= p2[101522688.1906242] Out[4]//NumberForm= "101,522,688.190,624,200" Unfortunately, the digit blocks extend to the decimal part, which is not very nice. 3) Now, I found myself in the same difficult situation some time ago, and had to struggle a little in order to get the desired results. In the end, I had to define a function, which I call p7 (an unusual symbol which will seldom interfere with other symbols you are using), and which will give you a nice printed result when you apply it to a number or list of numbers. The definition is rather lengthy, and I hope the moderator will let it go through. Otherwise, since I don't have your e-mail address, let me know it and I'll send it directly to you.. In[5]:= Clear[p7]; p7[y_ /; IntegerQ[y]] := Module[{x = Abs[y], c1, digs, ints, commaPositions}, ints = Length[IntegerDigits[Round[x]]]; digs = Characters[ToString[x]]; commaPositions = Reverse[Length[Take[digs, ints]] - Table[3 j, {j, 1, Floor[ints/3]}]]; If[y < 0, "-$", "$"] <> If[commaPositions == {}, StringJoin @@ digs <> ".00", StringInsert[StringJoin @@ Take[digs, ints], ",", If[commaPositions[[1]] == 0, Rest[commaPositions + 1], commaPositions + 1]] <> ".00"]]; p7[y_ /; Abs[y] < 999999] := Module[{x = Abs[N[y]], ents, decs}, digs = Characters[ToString[NumberForm[x, 16]]]; {ents, decs} = {Take[digs, Position[digs, "."][[1, 1]]], Take[digs, Position[digs, "."][[1, 1]] - Length[digs]]}; If[y < 0, "-$", "$"] <> If[Length[ents] < 5, StringJoin @@ ents , StringInsert[StringJoin @@ ents, ",", -5]] <> If[decs != {}, If[OddQ[Length[decs]], StringJoin[decs] <> "0", StringJoin @@ decs], "00"]]; p7[y_ /; Mod[y, 1] == 0] := Module[{x = y + Sign[y]*0.1, ints}, ints = Length[IntegerDigits[Round[x]]]; digs = Drop[Characters[ToString[NumberForm[x/10^ints, 12]]], If[x < 0, 3, 2]]; commaPositions = Reverse[Length[Take[digs, ints]] - Table[3 j, {j, 1, Floor[ints/3]}]]; If[y < 0, "-$", "$"] <> StringInsert[StringJoin @@ Take[digs, ints], ",", If[commaPositions[[1]] == 0, Rest[commaPositions + 1], commaPositions + 1]] <> ".00"]; p7[y_ /; Abs[y] >= 999999] := Module[{x = Abs[N[y]], c1, digs, ints, commaPositions}, ints = Length[IntegerDigits[Round[x]]]; c1 = ToString[NumberForm[x/10^ints, 20]]; digs = Drop[Characters[c1], 2]; commaPositions = Reverse[Length[Take[digs, ints]] - Table[3 j, {j, 1, Floor[ints/3]}]]; If[y < 0, "-$", "$"] <> StringInsert[StringJoin @@ Take[digs, ints], ",", If[commaPositions[[1]] == 0, Rest[commaPositions + 1], commaPositions + 1]] <> "." <> StringJoin @@ Take[digs, ints - Length[digs]] <> If[OddQ[Length[digs] - ints], "0", ""]]; You may find it useful to make this function listable: In[6]:= SetAttributes[p7, Listable]; Then you obtain: In[9]:= Clear[a]; a = p7[{-0.001, 0, 0., 12, 87.429, -123.4567893, 3245., -27322, 187500.00, -999998.9999, 999999.1, -1000000, -2322345.09875, 101522688.1906242, 123456543214.987654}] Out[9]= {"-$0.0010", "$0.00", "$0.00", "$12.00", "$87.4290", "-$123.45678930", \ "$3,245.00", "-$27,322.00", "$187,500.00", "-$999,998.9999", "$999,999.10", \ "-$1,000,000.00", "-$2,322,345.098750", "$101,522,688.19062420", \ "$123,456,543,214.9876"} Furthermore, there is the possibility of having this printed in a nice way. Try the following (the output is ommitted): In[10]:= result = {{"Value1", a[[1]]}, {"Value2", a[[2]]}, {"Value3", a[[3]]}, {"Value4", a[[4]]}, {"Value5", a[[5]]}, {"Value6", a[[6]]}, {"Value7", a[[7]]}, {"Value8", a[[8]]}, {"Value14", a[[14]]}}; StylePrint[DisplayForm[StyleBox[GridBox[result, GridFrame -> 2, RowLines -> True, ColumnLines -> True, ColumnAlignments -> {Right}], Background -> GrayLevel[0.9], FontSize -> 11, FontWeight -> Plain]]] Tomas Garza Mexico City ----- Original Message ----- From: "Felipe" <no at spam.com> To: mathgroup at smc.vnet.net Subject: [mg27495] [mg27475] Number formatting for financial applications > > I am having a hard time getting the format I need in my output. For some > reason Mathematica keeps putting things in engineering form when I just want > a long number > > e.g. I want to display this number > > 101522688.1906242 > > like this > > $ 101,522,688.1906242 (I can do without the $ if it's too hard) > > How can I achieve this without writing ten different tags each time. I just > want to have a formula with this output instead of having to wrap the > formula with AccountingForm[], NumberForm[], DigitBlock, etc. It looks > really ugly when I print it. > > thanks > Felipe > > >