7 ms·
Real decimal type - invaluable when working with currency What does JavaScript do with this though, just cast it to a float?
by kyllo 10y ago
Real decimal type - invaluable when working with currency
What does JavaScript do with this though, just cast it to a float?
- troyk 10y agomove the decimal and use an int (cents in US). It still blows me away that javascript has become so popular on the server without 64bit int types.
- justin_oaks 10y agoDo you really need those extra 11 bits? Javascript numbers accurately represent integers up to 2^53 - 1. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Cshelton 10y agoI just did some ownership percentage stuff where it's not uncommon to go 16 decimal places out...working with JavaScript on this was a pain. Never thought I'd care about that .00000000000001 difference hah...
- tlarkworthy 10y agofloating point works best near 0, most of the numbers it can represent lie near 0 (negative exponent is a number less than 1).
- snuxoll 10y agoUntil you need to deal with decimals instead of floats, then you are going to hate yourself because you have to pull in some third party library because the language treats every single number as a float (and floating point errors are a lot more common than most people think even when they are adding together simple numbers).
- Dylan16807 10y agoIntegers will do no better at pretending to be a decimal type without a library.
- snuxoll 10y agoSure, but most other languages have built-in support for decimal types. Java has BigDecimal, as does Ruby, Python has the decimal module, C# has System.Decimal, the list goes on. Javascript doesn't even have proper integers to guarantee the functioning of this correctly, it's a really sad state.
- Dylan16807 10y ago>Javascript doesn't even have proper integers to guarantee the functioning of this correctly I don't understand what you mean at all. Javascript should be able support a decimal library just as easily as a language with integers.
- kyllo 10y agoJavaScript doesn't have any int types at all. Your solution works until you need to do division...
- leef 10y agoIon has functions to turn Ion into JSON which will, of course, lose information. Annotations are dropped, decimals turn into a JSON number type which may lose precision, etc.
- wyc 10y agoI find that many financial technology companies opt to store currency as strings. The small overhead is typically well worth freedom from floating-point errors.
- Cshelton 10y agoCorrect. We store as strings then derive a number from the string for sorting purposes.
- deleted 10y ago[deleted]
- kazinator 10y agoAny text format is a technically a string. Just because some numeric token has no double quotes around it doesn't mean it isn't a string (in that representation). It's just that we can have some lexical rules that if that piece of text has the right kind of squiggly tail or whatever, it is always treated as a decimal float instead of every programmer working with it individually having to deal with an ad hoc string to decimal type conversion.
- kyllo 10y agoDon't they have to convert it back to a float to do math operations on it (if they're using JavaScript)?
- buro9 10y agoThe real way is: "price": { "amount": "1500", "scale": 2, "symbol": "GBP", } Currency has 3 properties, the amount, scale, and symbol. Amount is a string, it holds a bigint. Yes, it's a string. The value of Scale can be up to 5 but is usually 2 or 3. Symbol is the ISO code. Whenever I see a financial system that uses "amount": 15.00 I know that the system is ill-conceived.
- oblio 10y agoWhat's the scale in this context? Your explanation doesn't really clarify.
- pavlov 10y agoI guess "scale" here is log10 of the multiplier used in storing "amount". So to convert the value back into the currency range, you're supposed to compute "amount / 10^scale".
- buro9 10y agoFrom two different comments on here: http://stackoverflow.com/questions/5689369/what-is-the-difference-between-precision-and-scale http://stackoverflow.com/questions/5689369/what-is-the-diffe... Precision is the number of significant digits. Oracle guarantees the portability of numbers with precision ranging from 1 to 38. Scale is the number of digits to the right (positive) or left (negative) of the decimal point. The scale can range from -84 to 127. Worth noting this isn't specifically an Oracle thing, most financial systems need to be sure that it can store currency numbers accurately and this convention is widely used to ensure this. And: Precision 4, scale 2: 99.99 Precision 10, scale 0: 9999999999 Precision 8, scale 3: 99999.999 Precision 5, scale -3: 99999000 Typically when dealing with currencies scale is only used to represent the units less than whole unit of the currency, i.e. cents and pence. But there isn't anything that restricts it from being used to accommodate larger numbers with the use of negative scales. A current list of all ISO 4217 codes and the currency properties can be found here http://www.currency-iso.org/en/home/tables/table-a1.html http://www.currency-iso.org/en/home/tables/table-a1.html i.e. <CcyNtry> <CtryNm>UNITED STATES OF AMERICA (THE)</CtryNm> <CcyNm>US Dollar</CcyNm> <Ccy>USD</Ccy> <CcyNbr>840</CcyNbr> <CcyMnrUnts>2</CcyMnrUnts> </CcyNtry> The CcyMnrUnts property denotes 2 decimal places for the sub-unit of the dollar. So for the above example of 99999.999 you would store an amount of 99999999 and a scale of 3.