3 ms·
Python has decimals for example as do many other languages. C# decimal is used everywhere currency is used (I really hope). But apart from currency... why would
by viraptor 1y ago
Python has decimals for example as do many other languages. C# decimal is used everywhere currency is used (I really hope). But apart from currency... why would you use base 10? We've got native bigints in lots of languages so you don't even have to care how they're represented internally.
- arn3n 1y agoNot all decimal numbers have finite binary representations. 0.3, for example, is 0.010011 with a repeating block of 0011. For some business applications, you know you're being given a base 10 decimal of finite (but possibly very large) length.
- eru 1y agoYou can use an arbitrary length integer to store the number of cents? (Or whatever your smallest unit is.) Many languages also have libraries (or standard libraries) for supporting arbitrary length and precision rational numbers.
- viraptor 1y agoDecimals are always represented in a way that preserves the exact value. That doesn't mean internally it needs to be stored in base10. There's lots of ways to achieve that.
- eru 1y agoI'm not quite sure why anyone would need a 'decimal' data type for currencies? Couldn't you just express everything in eg cents, if you want that? Otherwise, many languages also have libraries for working with exact rational numbers.
- viraptor 1y agoYou're kind of asking why would anyone need decimal data, if they can implement their own decimal data with explicit decimal shifts around the codebase... The answer is - so they don't have to do it.
- koblas 1y agoSome countries don't have cents, some countries have 3 digits of cents. So, yes you can but you also then get thinking you can express everything as ints. But, the moment you need to convert from KWD to VND (84000.00:1) you could be at the edge of the size of a 64 bit int. Note: VND is pretty stable, when currencies start deflating the numbers can get crazy.
- eru 1y agoWell, obviously you put the smallest unit in your integers. Cents was just an example. Old British currency used to be even crazier than your example of a unit divided into 1,000. > But, the moment you need to convert from KWD to VND (84000.00:1) you could be at the edge of the size of a 64 bit int. So? You should use arbitrary length integers for that. Many languages have good support for them. Eg in Python they are the default integer you get.