Color scheme: Light
You are here: start » en » Documentation » Microsoft Access development tools » WKL Office JSON » WKL Office JSON how-to guides » How to: Read exact amounts and large identifiers

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

  • GetLong refuses a number with a fraction (jeTypeMismatch) instead of truncating it, and a number outside the Long range as well.
  • GetCurrency rounds more than four decimal places to four (banker's rounding, as CCur does); use GetDecimal for more decimal places.
  • Decimal holds 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: GetString returns all digits. Store such values in a text or decimal field, not in a Double field. 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" raise jeTypeMismatch - read such codes with GetString.
en/wiki/msaccess/json/how-to/read-exact-numbers.txt · Last modified: by Wolfgang van der Stille