3 ms·
Splitting the integer and the decimal part is always annoying because now you have to deal with non-normalized quantities. An option is to just use a custom flo
by gpderetta 5y ago
Splitting the integer and the decimal part is always annoying because now you have to deal with non-normalized quantities. An option is to just use a custom float type:
class AmountOfMoney
int mantissa
int exponent # base 10
- Archelaos 5y agoThis is a missunderstanding. If we have for example USD 10.42, the values in my implementation are: FinerAmount = 1042 CoarserAmount = 10.42 The profile of the class was abbreviated. Of course, we also need a currency property for the general case: Currency = USDollar The definition of the class for the currency in pseudo-code: class Iso4217CurrencyCode int NumericCode string AlphabeticCode int DecimalPlaces static USDollar = new Iso4217CurrencyCode(840, "USD", 2); static Yen = new Iso4217CurrencyCode(392, "JPY", 0); The constructor for the amount of money only uses FinerAmount, and CoarserAmount is calculated: CoarserAmount => Convert.ToDecimal(FinerAmount / Math.Pow(10, Currency.DecimalPlaces)); So foo = AmountOfMoney(FinerAmount: 1042, Currency: USDollar); sets foo.CoarserAmount automatically to 10.42. But bar = AmountOfMoney(FinerAmount: 1042, Currency: Yen); sets bar.CoarserAmount automatically to 1042.