How to: Read exact amounts and large identifiers
Read money amounts without floating-point rounding and identifiers with more digits than a
Long or Double holds.
Solution overview
The library keeps every number as the text it was read with. GetCurrency and GetDecimal
convert that text without a detour through Double (Currency then rounds to four decimal places, Decimal to 28-29
significant digits); Value returns a Decimal for whole numbers beyond the Long range. Only GetDouble, and
Value for numbers with a fraction or an exponent or beyond the Decimal range, produce a Double.
Implementation
Dim doc As New JDoc
doc.LoadJson "{""total"": 1234567.89, ""orderId"": 9007199254740993}"
Dim total As Currency
total = doc.GetCurrency("$.total") ' 1234567.89, exact
Debug.Print doc.GetDecimal("$.orderId") ' 9007199254740993
Debug.Print TypeName(doc.Root("orderId").Value) ' Decimal
' Write an amount exactly
doc.SetValue "$.total", CCur(99.95)
Remarks
GetLongrefuses a number with a fraction (jeTypeMismatch) instead of truncating it, and a number outside theLongrange as well.GetCurrencyrounds more than four decimal places to four (banker's rounding, asCCurdoes); useGetDecimalfor more decimal places.Decimalholds 28 to 29 significant digits; a number with more digits is rounded to that precision without an error, and a magnitude below 1E-28 becomes 0. The JSON text itself is never changed:GetStringreturns all digits. Store such values in a text or decimal field, not in aDoublefield. All rules and limits: Values and limits.- Numbers inside strings are read when their text without surrounding spaces is a JSON number:
"42"and" 4.2 "are read;"007","+5",".5"and"12,5"raisejeTypeMismatch- read such codes withGetString.
Related topics
en/wiki/msaccess/json/how-to/read-exact-numbers.txt · Zadnja sprememba: uporabnika Wolfgang van der Stille