Live data from Hacker News

What even is a JSON number?

blog.trl.sn

121–130 of 151 posts

Re: What even is a JSON number?

#122
post #110
post #106

Earlier 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…

Implementation of fixed point decimals, using multiple integer representations encoded within a floating point system. Nice.

Re: What even is a JSON number?

#123
post #110

Earlier 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.

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?

#125
post #51

Earlier 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.

In JS, it's a good idea anyway to use some JSON parsing library instead of JSON.parse.

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?

#126
post #123

Earlier 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.

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?

#127

Long 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.

Then you should store the time as well, because the number of decimals in a currency can change (see ISK). Also, some systems disagree on the number of decimals, so be careful. And of course prices can have more decimals. And then you have cryptocurrencies, so make sure you use bigints

Re: What even is a JSON number?

#128
post #109
post #108

Earlier 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.

Your software will still "lose" cents if you use integers, for operations such as dividing a bill (e.g. divide by 3), or applying 3% APR in monthly increments.

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?

#129
post #123

Earlier 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.

To be clear, I wasn't advocating for flexible decimal points. There is no "dp" parameter in the solution I was proposing. It's just documented in the API that "price" is denominated in cents (or satoshis or whatever you want)

Re: What even is a JSON number?

#130
post #67

Earlier 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 a 64 bit integers because you've got some super high precision math you need to do over an enormous space of addressable numbers, like maybe you're firing unguided kinetic energy weapons at enemies on other planets... sure, use big numbers. I'm sure you've got some clever libraries able to do such things reliably, and I won't question why you're using json as your serialization format.

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.

Post reply on HN