Stream: fhirpath
Topic: Rounding a decimal?
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)?
Bryn Rhodes (Sep 26 2018 at 14:48):
.toInteger()
Bryn Rhodes (Sep 26 2018 at 14:50):
Actually, no, that doesn't allow decimal to integer.
Bryn Rhodes (Sep 26 2018 at 14:50):
So div
Paul Lynch (Sep 26 2018 at 14:56):
Wouldn't div truncate rather than round?
Paul Lynch (Sep 26 2018 at 14:57):
Why doesn't toInteger accept a decimal?
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.
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.
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?
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)
Bryn Rhodes (Sep 26 2018 at 15:19):
Note that div and mod support decimals, not just integers.
Paul Lynch (Sep 27 2018 at 20:54):
nicola (RIO/SS) (Sep 30 2018 at 17:56):
We have a demo of fhirpath.js in Columbia room in 5 min - join us!
nicola (RIO/SS) (Sep 30 2018 at 17:57):
https://lhncbc.github.io/fhirpath.js/
nicola (RIO/SS) (Sep 30 2018 at 17:58):
@Josh Mandel would you like to join us?
Josh Mandel (Sep 30 2018 at 21:13):
Hey -- sorry to have missed it!
Last updated: Apr 12 2022 at 19:14 UTC