I was recently bitten by this wrong "scaled decimal expectation", in which what I thought was wrong it actually wasn't, or I can't tell whether it was.
I would expect that a scaled decimal would be something like what Esteban L. suggests, an integer where you just move the decimal point for printing, and also a number where you lose precision due to truncation. So this: ((1.0s2 / 3) * 3) + 0.01s2 Would yield 1 instead of "1.01s2" So if you use the "integer only" representation, it is very much like any currency number would me managed: ((100 / 3) truncated * 3 + 1) / 100 This, of course, yields the right value, but it started truncated from the beginning. (0.33s2 * 3) + 0.01s2 "1" I would appreciate any pointers about learning more about this. It's embarrassing for me not knowing how each type should work in order to tell what's right from what's wrong. Regards! ps: In my case this wasn't because of money, but another kind of use, but the situation is the same. Esteban A. Maringolo On Mon, Aug 30, 2021 at 9:51 AM Esteban Lorenzano <esteba...@netc.eu> wrote: > > hi, > > some time ago there was a "money" package around, that basically had a > Currency type that internally stored values as integers (like, 42.0 was > becoming 4200). This had some advantages to make this kind of calculus (also, > it has the "other" advantage of making you believe that you have a lot more > money than what you have ;). > > maybe you need to go that direction? > > Esteban > > On Aug 30 2021, at 2:31 pm, Sven Van Caekenberghe <s...@stfx.eu> wrote: > > David, > > > On 30 Aug 2021, at 14:02, David Pennington <da...@totallyobjects.com> wrote: > > > > Hi everyone. I have a little bank analysis package for my own use but > > having trouble displaying and storing amounts. I parse out a CSV file from > > the bank and convert the amounts from text to ScaledDecimal. I then store > > these into a STON file, which converts them back to text. I then read them > > in and convert them back to ScaledDecimal again. > > > > I am not used to ~Pharo have spent 24 years using VisualAge Smalltalk so I > > need a little bit of help because I am getting 1 Penny errors in the > > conversions. I can cope with this but I would like to get it right. > > > > Can anyone give me a simple means of managing, say, an amount like £76.49 > > from the bank so that it stops coming back to me as £76.48? > > > > David > > Totally Objects > > Working with money is always challenging. I know that many people say 'Use > ScaledDecimal' as if that magically solves all problems but it does not. > ScaledDecimal is a bit of a dangerous class to use. It is just a fraction > with a certain precision. Internally it might be pretty accurate with respect > to most operations, its external representation as floating point number can > be misleading, as this representation is prone to all problems related to > floating point (including the fact that decimal and binary floating point are > not the same). > > STON does not doing anything wrong as far as I know. Consider the following: > > | amount ston | > amount := 76.49 asScaledDecimal. > ston := STON toString: amount. > STON fromString: ston. > (STON fromString: ston) = amount. > "true" > > | amount ston | > amount := 76.49 asScaledDecimal: 2. > ston := STON toString: amount. > STON fromString: ston. > (STON fromString: ston) = amount. > "true" > > | amount ston | > amount := 76.49. > ston := STON toString: amount. > STON fromString: ston. > (STON fromString: ston) = amount. > "true" > > BUT, 76.49 asScaledDecimal already has your penny loss (if rounded to > pennies). > > HTH, > > Sven