This "Averia Serif Libre" is unreadable for me.
What even is a JSON number?
121–130 of 151 posts
Re: What even is a JSON number?
#122Earlier quoted context omitted.
What happens if you’re sure that four decimal places is the smallest, then suddenly a partner system starts sending you 6 decimal places?
Precision should be part of the spec for integrations. With the integer multiple of minimal unit, that makes it clear in the API what it is. e.g. it doesn't make sense to support billing in sub-currency unit amounts just by allowing it in your API definition, as you're going to need to batch that until you get a billable amount which is larger than the fee for issuing a bill. Even for something like $100,000.1234, th…
Re: What even is a JSON number?
#123Earlier quoted context omitted.
Precision should be part of the spec for integrations. With the integer multiple of minimal unit, that makes it clear in the API what it is. e.g. it doesn't make sense to support billing in sub-currency unit amounts just by allowing it in your API definition, as you're going to need to batch that until you get a billable amount which is larger than the fee for issuing a bill. Even for something like $100,000.1234, th…
Implementation of fixed point decimals, using multiple integer representations encoded within a floating point system. Nice.
If you're doing ads and going for millicents, something like $0.01234 should be encoded as 1234 millicents.
Obviously you have to agree on what you're measuring in the API, you can't have some values be millicents and others cents.
Re: What even is a JSON number?
#124Re: What even is a JSON number?
#125Earlier quoted context omitted.
If you're going to extend Go the courtesy of customizing the parser, oughtn't you do the same for Python (and all the languages)? To wit, Python's json module has `parse_float` and `parse_int` hooks: https://docs.python.org/3/library/json.html#encoders-and-dec... Example: >>> json.loads('{"int":12345,"float":123.45}', parse_int=str, parse_float=str) {'int': '12345', 'float': '123.45'} FWIW, when I've cared about inte…
I'm just a JS guy trying to understand the world around me and documenting what I find, not trying to be discourteous (or even courteous). I'll add the note about Python, thanks for calling it out. FWIW JS does not have a similar capability so I can't add a note there.
With Zod, you can use z.bigint() parser. If you take the "parse any JSON" snippet https://zod.dev/?id=json-type and change z.number() to z.bigint(), it should do what you are looking for.
Re: What even is a JSON number?
#126Earlier quoted context omitted.
Implementation of fixed point decimals, using multiple integer representations encoded within a floating point system. Nice.
Well I mean, if your minimal unit is 1c, then a price like $22.56 should be encoded as 2256 cents. If you're doing ads and going for millicents, something like $0.01234 should be encoded as 1234 millicents. Obviously you have to agree on what you're measuring in the API, you can't have some values be millicents and others cents.
Re: What even is a JSON number?
#127Long story short: don't use JSON numbers to represent money or monetary rates. Always use decimals encoded as string. It's surprising how many APIs fall short of this basic bar.
No. Use integers to store the smallest money decimal, and store the currency name alongside.
Re: What even is a JSON number?
#128Earlier quoted context omitted.
For money, that's a sane setup. But do note, that in currency, there are multiple, actively used currencies that have zero, three, five (rare) or even eight (BTC) decimals. That some decimals cannot be divided by all numbers (e.g. only 0.5) Point being: floats are dangerously naive for currency. But integers are naive too. You'll most probably want a "currency" or "money" type. Some Value Object, or even Domain Model…
When I say dangerously naive, I mean in a way that people can go to jail¹ for "loosing" or "inventing" cents. Which your software will do if you use floats. ¹IANAL. But this was told when legal people looked at our architecture.
The goal is not to avoid rounding errors (which would be quite difficult when the true account value can be an irrational number, as with 3% APR compounding monthly), but to have the exact same rounding errors that are prescribed by the accounting practices. Which may vary depending on legislation.
A decimal floating point is usually a better starting point than integers are.
Re: What even is a JSON number?
#129Earlier quoted context omitted.
Well I mean, if your minimal unit is 1c, then a price like $22.56 should be encoded as 2256 cents. If you're doing ads and going for millicents, something like $0.01234 should be encoded as 1234 millicents. Obviously you have to agree on what you're measuring in the API, you can't have some values be millicents and others cents.
Yeah I am more laughing that once encoded in JSON as { "p": 2256, "dp": 2 } you are using 2 floating point numbers. But JSON, and indeed JS wasn't designed.
Re: What even is a JSON number?
#130Earlier quoted context omitted.
Don't care. A numeric identity is an identity and so is a string. If you want to math it, it is a number, otherwise... string. "Will you ever want the 95th percentile PID? Then it is not a number. Move on."
Double precision floats can't represent every 64-bit integer. If you want to math it, what kind of number will you accept?
If you're using 64 bit numbers as a high cardinality identity that can be randomly generated without concern for collision (like a MAC address with more noise) -- well, that's an identity and doesn't need to have math applied to it. For example: "What's the mean IP address that's connected to cloudflare in the last 10 minutes" or "what's the sum of every mac address in this subnet?" are both nonsense properties because these "numbers" are identities not numbers, and using a data type that treats them as numbers invites surprising, sometimes unpleasantly so, results.
Of course, because these are computers, all strings are ultimately numbers but their numberness is without real meaning.