FHIR Chat · Rounding a decimal? · fhirpath

Stream: fhirpath

Topic: Rounding a decimal?


view this post on Zulip Paul Lynch (Sep 26 2018 at 14:44):

For a Questionnaire, I wrote a FHRIPath expression to do a BMI calculation, and the result was "22.142901142866". Does FHIRPath provide some facility for rounding a Decimal to some number of decimal places, or for converting it to an integer (which could be used to accomplish the same thing)?

view this post on Zulip Bryn Rhodes (Sep 26 2018 at 14:48):

.toInteger()

view this post on Zulip Bryn Rhodes (Sep 26 2018 at 14:50):

Actually, no, that doesn't allow decimal to integer.

view this post on Zulip Bryn Rhodes (Sep 26 2018 at 14:50):

So div

view this post on Zulip Paul Lynch (Sep 26 2018 at 14:56):

Wouldn't div truncate rather than round?

view this post on Zulip Paul Lynch (Sep 26 2018 at 14:57):

Why doesn't toInteger accept a decimal?

view this post on Zulip Bryn Rhodes (Sep 26 2018 at 15:03):

Yes, rounding would have to be accounted for if you wanted a round instead of a truncate.

view this post on Zulip Bryn Rhodes (Sep 26 2018 at 15:03):

Because there are multiple ways to change a decimal into an integer, so convert doesn't assume which one you want.

view this post on Zulip Paul Lynch (Sep 26 2018 at 15:07):

Okay, I guess I could do: "((long FHIRPath expression) + 0.5) div 1" to get a rounded integer value. It is not an obvious approach, though it should work. Maybe a "round()" function could be added?

view this post on Zulip Paul Lynch (Sep 26 2018 at 15:10):

It actually starts looking more complicated if you want to round not to an integer to but to fewer decimal places:

 (((long FHIRPath expression)*10 + 0.5) div 1)/10

to get 1 decimal place, as opposed to something like

(long FHIRPath expression).round(1)

view this post on Zulip Bryn Rhodes (Sep 26 2018 at 15:19):

Note that div and mod support decimals, not just integers.

view this post on Zulip Paul Lynch (Sep 27 2018 at 20:54):

GF#18501

view this post on Zulip nicola (RIO/SS) (Sep 30 2018 at 17:56):

We have a demo of fhirpath.js in Columbia room in 5 min - join us!

view this post on Zulip nicola (RIO/SS) (Sep 30 2018 at 17:57):

https://lhncbc.github.io/fhirpath.js/

view this post on Zulip nicola (RIO/SS) (Sep 30 2018 at 17:58):

@Josh Mandel would you like to join us?

view this post on Zulip Josh Mandel (Sep 30 2018 at 21:13):

Hey -- sorry to have missed it!


Last updated: Apr 12 2022 at 19:14 UTC