Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Precision

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129346] Re: Precision
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Mon, 7 Jan 2013 00:36:14 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20130105071925.AC08868CA@smc.vnet.net>

The issue is not in Mathematica's adding nor precision it is in the
default output display. The default output display is set to six
digits. You want dollars and cents (dnc) -- presumably only sometimes
-- so define a function to alter the output display when appropriate.

Clear[dnc]
dnc[x_?NumericQ]:=
    NumberForm[x,{Ceiling[Log[10,x]]+3,2}]


103971.66+52282.64+2998.27//dnc

159252.57


0.2//dnc

0.20


Or with comma separators

dnc[x_?NumericQ]:=
    NumberForm[x,{Ceiling[Log[10,x]]+3,2},
      DigitBlock->3]


103971.66+52282.64+2998.27//dnc

159,252.57


If you only work with currency then add dnc to $Post

?$Post

$Post is a global variable whose value, if set, is applied to every
output expression.  >>

Since dnc will be applied to all output, you need dnc to also handle
non-numeric output. i.e., ignore it

dnc[x_]=x;


??dnc

Global`dnc

dnc[x_?NumericQ]:=NumberForm[x,{Ceiling[Log[10,x]]+3,2},DigitBlock->3]

dnc[x_]=x


$Post=dnc;


103971.66+52282.64+2998.27

159,252.57


To clear $Post

$Post=.


Bob Hanlon


On Sat, Jan 5, 2013 at 2:19 AM, Rob Ryan <tio540s1 at gmail.com> wrote:
> My frustration is growing. I simply want to add (for example):
> 103971.66+52282.64+2998.27. These are dollars and cents and I'd like to k=
eep the cents. But when I input that line, I get 159923. When I input: N[(1=
03971.66+52282.64+2998.27),20] I STILL get 159923. What do I do to get 1599=
52.57? I've used "SetPrecision", etc. with still no result.
> I note that when I type 103971.66 and hit shift-return, the output is 103=
972. If I then type "InputForm[%]" I get 103971.66.
>
> There simply has to be an easy way to add these numbers - any calculator =
watch can do it!



  • References:
    • Precision
      • From: Rob Ryan <tio540s1@gmail.com>
  • Prev by Date: Re: Precision
  • Next by Date: Re: Precision
  • Previous by thread: Precision
  • Next by thread: Re: Precision