## beanshell-users

 [Beanshell-users] + - * / > < >= <= == for BigDecimal From: Kireitchik, Valeri - 2004-11-04 10:33:30 Attachments: Message as HTML ```Hi all, we are starting to use beanshell for writing many small scripts = implementing some business rulsed. Obviously, there are a lot of money = and money realated calculations there. Use of double is not possible due to not precise results. So we should = use BigDecimal. But with BigDeciamal we lack simple arithmetical = operators (+ - * / > < >=3D <=3D =3D=3D). And IMHO it hurts readability = of script and even the possiblity to use it by non-programmers. For examle compare: x =3D (a + b) * (c - d); and x =3D a.add(b).multiply(c.subtract(d)); Maybe somebody can advice me a way out ? Best regards Valeri Kireitchik ```
 RE: [Beanshell-users] + - * / > < >= <= == for BigDecimal From: Shankar Unni - 2004-11-04 17:34:28 Attachments: Message as HTML ```Traditionally, the solution has been to use a type like "long" with a fixed scale factor. E.g. here in the US, if, for argument's sake, we need to perform computations to \$0.0001 (== 1/100 of 1 cent) accuracy, we just represent all amounts as Dollars * 10000 + Cents * 100. Computations can result in numbers with fractional cents, which can be rounded to nearest *for display purposes only* (or for exchange with external systems) - internally, they would be maintained in this representation. With the range of values of "long", this gives a range of upto \$922,337,203,685,477.58 (\$922 quadrillion), which is more than enough to cover the entire monetary supply of the world. So all you have to do is to perform the computations in these units (we called them "centi-cents" in a previous job of mine), and do the obvious "rounding" for display *only* (internally, everything is maintained in centi-cents). Depending on the level of rounding error that you can tolerate (and the number of division operations you perform on a typical quantity), you can adjust the fractional digits appropriately - there are plenty to spare. So my advice is: ditch the BigDecimal, and use longs for amounts. _____ From: beanshell-users-admin@... [mailto:beanshell-users-admin@...] On Behalf Of Kireitchik, Valeri Sent: Thursday, November 04, 2004 2:33 AM To: Beanshell Mail List (??. ?????) Subject: [Beanshell-users] + - * / > < >= <= == for BigDecimal Hi all, we are starting to use beanshell for writing many small scripts implementing some business rulsed. Obviously, there are a lot of money and money realated calculations there. Use of double is not possible due to not precise results. So we should use BigDecimal. But with BigDeciamal we lack simple arithmetical operators (+ - * / > < >= <= ==). And IMHO it hurts readability of script and even the possiblity to use it by non-programmers. For examle compare: x = (a + b) * (c - d); and x = a.add(b).multiply(c.subtract(d)); Maybe somebody can advice me a way out ? Best regards Valeri Kireitchik ```