JSON numeric literals conform to the regular expression:
-?(0|[1-9][0-9]*)(\.[0-9]+)?([Ee][+\-]?[0-9]+)?
In other words there is an optional leading negative sign, one or more digits (leading zeroes are not allowed), an optional decimal part and an optional exponent. Note that it is not permissible to omit the leading digit on a decimal e.g. '.123', to omit trailing digits after a decimal e.g. '456.' or use a leading plus sign e.g. '+789'.
The easiest way to count these is by separately considering formats with and without decimals, formats with and without exponents and, at the highest level, formats with and without the leading negative sign.
Throughout the following, N is string length. This is fully unambiguous because all the characters in use are pure ASCII.
These have neither a decimal point nor an exponent.
| N | Count | Examples |
|---|---|---|
| 0 | 0 | none |
| 1 | 10 | '0' to '9' |
| 2 | 90 | '10' to '99' |
| 3 | 900 | '100' to '999' |
| 4 | 9,000 | '1000' to '9999' |
| 5 | 90,000 | '10000' to '99999' |
| ≥ 2 | 9 × 10N - 1 | ... |
These have a decimal part but no exponent. Bear with me on how these are enumerated, it makes the calculation more logical...
| N | Count | Examples |
|---|---|---|
| 0 | 0 | none |
| 1 | 0 | none |
| 2 | 0 | none |
| 3 | 100 |
'0.0' to '0.9' (10)'1.0' to '9.9' (90) |
| 4 | 1,900 |
'0.00' to '0.99' (100)'1.00' to '9.99' (900)'10.0' to '99.9' (900) |
| 5 | 28,000 |
'0.000' to '0.999' (1,000)'1.000' to '9.999' (9,000)'10.00' to '99.99' (9,000)'100.0' to '999.9' (9,000) |
| 6 | 370,000 |
'0.0000' to '0.9999' (10,000)'1.0000' to '9.9999' (90,000)'10.000' to '99.999' (90,000)'100.00' to '999.99' (90,000)'1000.0' to '9999.9' (90,000) |
| ≥ 3 |
(1 + 9(N - 2)) × 10N - 2 = (9N - 17) × 10N - 2 |
... |
These have an unsigned exponent but no decimal part. Results are multiplied by 2 because there are two possible unsigned exponent symbols, 'E' and 'e'.
| N | Count | Examples |
|---|---|---|
| 0 | 0 | none |
| 1 | 0 | none |
| 2 | 0 | none |
| 3 | 200 |
|
| 4 | 3,800 |
|
| 5 | 56,000 |
|
| 6 | 740,000 |
|
| ≥ 3 |
(1 + 9(N - 2)) × 10N - 2 × 2 = (18N - 34) × 10N - 2 |
... |
These have a signed exponent but no decimal part. Results are multiplied by 4 because there are four possible signed exponent symbols, 'E+', 'E-', 'e+' and 'e-'.
| N | Count | Examples |
|---|---|---|
| 0 | 0 | none |
| 1 | 0 | none |
| 2 | 0 | none |
| 3 | 0 | none |
| 4 | 400 |
|
| 5 | 7,600 |
|
| 6 | 112,000 |
|
| 7 | 1,480,000 |
|
| ≥ 4 |
(1 + 9(N - 3)) × 10N - 3 × 4 = (36N - 104) × 10N - 3 |
... |
These have a decimal part and an unsigned exponent. Results are multiplied by 2 because there are two possible unsigned exponent symbols, 'E' and 'e'. The calculation is more complicated because there are O(N2) combinations of locations for the decimal point and the exponent symbol.
| N | Count | Examples |
|---|---|---|
| 0 | 0 | none |
| 1 | 0 | none |
| 2 | 0 | none |
| 3 | 0 | none |
| 4 | 0 | none |
| 5 | 2,000 |
|
| 6 | 58,000 |
|
| 7 | 1,140,000 |
|
| ≥ 4 |
((N - 4) + 9(N - 3)(N - 4)/2) × 10N - 3 × 2 = (9N2 - 61N + 100) × 10N - 3 |
... |
These have a decimal part and a signed exponent. Results are multiplied by 4 because there are four possible signed exponent symbols, 'E+', 'E-', 'e+' and 'e-'. The calculation is more complicated because there are O(N2) combinations of locations for the decimal point and the exponent symbol.
| N | Count | Examples |
|---|---|---|
| 0 | 0 | none |
| 1 | 0 | none |
| 2 | 0 | none |
| 3 | 0 | none |
| 4 | 0 | none |
| 5 | 0 | none |
| 6 | 4,000 |
|
| 7 | 116,000 |
|
| 8 | 2,280,000 |
|
| ≥ 5 |
((N - 5) + 9(N - 4)(N - 5)/2) × 10N - 4 × 4 = (18N2 - 158N + 340) × 10N - 4 |
... |
This is simply the sum of the previous six tables.
| N | Count |
|---|---|
| 0 | 0 |
| 1 | 10 |
| 2 | 90 |
| 3 | 1,200 |
| 4 | 15,100 |
| 5 | 183,600 |
| 6 | 2,184,000 |
| 7 | 25,536,000 |
| 8 | 294,480,000 |
| ≥ 5 | (108N2 + 2292N + 4200) × 10N - 4 |
That is, numeric literals with a negative sign, such as -123.456E+789.
The number of negative JSON numeric literals of length N is simply the number of non-negative numeric literals of length N - 1. If N is 0 the count is 0.
| N | Count |
|---|---|
| 0 | 0 |
| 1 | 0 |
| 2 | 10 |
| 3 | 90 |
| 4 | 1,200 |
| 5 | 15,100 |
| 6 | 183,600 |
| 7 | 2,184,000 |
| 8 | 25,536,000 |
| 9 | 294,480,000 |
| ≥ 6 | (108N2 + 2076N + 2016) × 10N - 5 |
This is simply the sum of the previous two tables.
| N | Count |
|---|---|
| 0 | 0 |
| 1 | 10 |
| 2 | 100 |
| 3 | 1,290 |
| 4 | 16,300 |
| 5 | 198,700 |
| 6 | 2,367,600 |
| 7 | 27,720,000 |
| 8 | 320,016,000 |
| 9 | 3,652,080,000 |
| ≥ 6 | (1188N2 + 24996N + 44016) × 10N - 5 |
There are 198,700 JSON numeric literals of length 5. These are:
183,600 non-negative numeric literals:
'10000' to '99999''0.000' to '9.999''10.00' to '99.99''100.0' to '999.9''0E000' to '9E999''0e000' to '9e999''10E00' to '99E99''10e00' to '99e99''100E0' to '999E9''100e0' to '999e9''0E+00' to '9E+99''0E-00' to '9E-99''0e+00' to '9e+99''0e-00' to '9e-99''10E+0' to '99E+9''10E-0' to '99E-9''10e+0' to '99e+9''10e-0' to '99e-9''0.0E0' to '9.9E9''0.0e0' to '9.9e9'15,100 negative numeric literals:
'-1000' to '-9999'-0.00' to '-9.99'-10.0' to '-99.9'-0E00' to '-9E99'-0e00' to '-9e99'-10E0' to '-99E9'-10e0' to '-99e9''-0E+0' to '-9E+9''-0E-0' to '-9E-9''-0e+0' to '-9e-9''-0e-0' to '-0e-9'