FHIR Chat · typescript - decimal support · javascript

Stream: javascript

Topic: typescript - decimal support


view this post on Zulip Brian Postlethwaite (Jun 22 2021 at 07:44):

Not directly a typescript issue, but anyone else got any good tips on parsing decimal data in a way that doesn't loose the precision?
parseFloat removes all the trailing precision from it...
I'm using it in my Javascript Questionnaire renderer

view this post on Zulip Brian Postlethwaite (Jun 22 2021 at 07:50):

Anyone tried this module? https://github.com/globalizejs/globalize#number-module

view this post on Zulip Chris Moesel (Jun 22 2021 at 12:55):

I have not. You might also consider big.js (16M downloads/week) or bignumber.js (5M downloads/week) -- but I have not tried either of those either.

view this post on Zulip Gino Canessa (Jun 22 2021 at 16:47):

Brian, I'm usually pretty good at remembering that issue, but I haven't looked at it in JS yet.

I think the problem Brian is asking about is in the JSON parser / serializer; e.g., on chromium-based browsers you get the following behavior:

JSON.parse('{"data":3.000000}')
 -> returns: {data: 3}
JSON.stringify({ data: 3.0000})
 -> returns: "{\"data\":3}"

Both of which have lost the additional precision on the number... sadly, that is conformant with the spec :sigh:.

For serialization, it may work to use one of those libraries an add a custom replacer function for JSON.stringify (looks good on paper, haven't tested).

Unfortunately, the parser is harder to work around. Even using the reviver parameter on JSON.parse doesn't help because the value passed in is already a number by the time the function is called. I would probably start with the polyfill implementation on the Mozilla page or an existing C/C++ library I could compile via Emscripten (or possibly C# via h5, but I have never used it so I don't know if it actually works =). Time permitting, I'd like to see a few options and performance test them, but that feels like a lot unless the first attempt had horrible performance.

I'll add it to my (super short, really) list, unless someone else has solved this already (:hopeful look:)?

view this post on Zulip Chris Moesel (Jun 22 2021 at 19:15):

I hadn't thought of it in the context of deserializing a whole JSON object, but yes, in that case it's a bit more complicated than just "use this library". That said, the original post said "parseFloat removes all the trailing precision from it", so if he is talking about parseFloat, then maybe he is really working w/ the numeric values directly... But of course I am not going to stop you, @Gino Canessa, if you want to spend some time solving for the JSON problem too!

view this post on Zulip Gino Canessa (Jun 22 2021 at 19:26):

Lol @Chris Moesel =). I guess it could be in either point, since each step along the way will lose precision.

Guess this is relevant to the larger 'standard FHIR library for JS/TS', since it will need to include a parser/serializer and some object library for precise numeric types (I like your suggestion of big.js).

view this post on Zulip Josh Mandel (Jun 22 2021 at 22:00):

https://gitlab.com/shimaore/jsonparse-decimal is the kind of thing that we're talking about, yes? (Integrates a JSON parser + decimal handling)

view this post on Zulip Josh Mandel (Jun 22 2021 at 22:01):

(That particular library doesn't seem super active or popular; just looking at the shape of the solution...)

view this post on Zulip Gino Canessa (Jun 22 2021 at 22:10):

Yes - it has the parser and the dependency on the (also not very active) decimal-js.light project.

view this post on Zulip Brian Postlethwaite (Jun 23 2021 at 04:55):

All of these issues yes, but the specific spot I was slipping on was the conversion of user entered text data into the fhir questionnaire answer as valueDecimal...


Last updated: Apr 12 2022 at 19:14 UTC