I could really use some help learning how to use the below function.
I looked at the EMR help and am not successful in getting this function to work. I don't really understand the example given.
{fn RND_HT( ){ //define function
local hgt = val(OBSANY("HEIGHT")
round(hgt,0)}}
does anyone have some insight or help to offer?
-richard
it looks like you are trying to use round(), the function takes a numerical value and rounds it to the specified number of decimal places. There are two arguments, the first is the value to be rounded (it must be a value, not a string) and the second is the number of decimal points (again, a numerical value, not a string)
These are incorrect -
round("74.0090",2)
round("74.0090","2")
round(74.0090,"2")
This is correct -
round(74.0090,2)
If you are trying to round an observation or document variable, it is likely stored as a string. If you do this -
round(val(document.variable),2)
That will probably work for you. Val() converts a string to a value just as str() converts a value to a string. Hope that helps
gibsonmi said:
If you are trying to round an observation or document variable, it is likely stored as a string. If you do this -
round(val(document.variable),2)
That will probably work for you. Val() converts a string to a value just as str() converts a value to a string. Hope that helps
It definitely looks helpful. So, to be more clear. If I wanted to round a variable: in the "white area" of VFE I would put something like
{if DOCUMENT.VALUE1 <> "" then DOCUMENT.VALUE2 = round(val(document.VALUE1),2) etc... }
Am I on the right track?
Ok so, I got it to round to the nearest 100th, which is nice, but I still have zeros trailing past the second decimal. so, the value actual value is 1.029123456789 and the round() returns 1.03000000000. Is there a way for me to format this so the number looks like 1.03?
Thanks again for the help!
truncate
description
Shortens a value to the specified decimal point.
type
MEL utility function
syntax
truncate(value, point)
arguments
value The value to truncate
point The number of spaces after the decimal point after truncating
returns
Truncated number
comment
N/A
example
{fn TNC_HT(){ //define function
local ht = val(OBSANY("Height"))
truncate(ht,0)}}
{TNC_HT( )} //call function
Returns 75 where OBSANY(’Height’) returns 75.75
The function defined in this example uses truncate() to return a truncated height value.
where used
Chart: templates for letters and handouts, text components, quick text
Encounter Form Editor: MEL expressions, data displays, text translations
I tried this too. Still, pretty much the same result. instead of 1.079123456, I get 1.070000000
so, I figure something is wrong with - {if document.variable1 <> "" then document.variable2 = truncate(val(document.variable1),2) else "" endif}
help please....
This should work for you
{fn RemoveEndZero(var){
//use a string for var
while sub(var,size(var),1) == "0" do
var = sub(var,1,size(var) - 1)
endwhile
return var
}}
{if document.variable1 <> "" then document.variable2 = RemoveEndZero(round(val(document.variable1),2)) else "" endif}
Actually, I should have converted that to a string first, my bad
{if document.variable1 <> "" then document.variable2 = RemoveEndZero(str(round(val(document.variable1),2))) else "" endif}
Oooooh! that works really well. Thank you very much!!!
Thanks again for the help. It turns out that the function above removes all end zeros, so even if the value has no decimals, the zeros do not show ex 10 = 1 (which is not good). So, I think I figured out the solution, but if you see something is wrong with what I am leaving below, please let me know... also, I decided to truncate instead of round. I am not sure if that makes a difference, though.
{fn RemoveEndZero(var){
while sub(var,size(var),1) == ".0" do
var = sub(var,1,size(var) - 1)
endwhile
return var
}}
{if DOCUMENT.VARIABLE1 <> "" then DOCUMENT.VARIABLE2 = RemoveEndZero(str(truncate(val(DOCUMENT.VARIABLE1),2))) else "" endif}
Yeah, it was looking at the last character and removing it if it was a zero. That wont work because you are asking it to remove the last character if it is equal to ".0" which it never will be because thats two characters. You could take a different approach, like pass a number of decimal places to keep to matter what, I just thought the zeros might be easier, I can see where that would have problems though. I dont understand why truncate wouldnt just actually truncate the number, but we can find a way around it.
Try something like this, didnt test it, but tried to explain what I was doing -
{fn DecimalPoints(var,num){
//use a string for var
//num is number of decimal points to keep, use a value
//if there is no decimal point return the string unchanged
if match(var,".")<1 then return var endif
//if it already has the specified number or less decimal points, return the string unchanged
if (match(var,".") + num) >= size(var) then
return var
else
//otherwise find the decimal, move num places to the right and return the string up to that point
return sub(var,1,match(var,".") + num)
endif
}}
That works really well.
At this point I have tried all the functions you've thrown my way. I really appreciate it.
I figured you would say what you said about the ".0", but regardless of it not suppose to be working, it works. Could you take another look at it or test it out and see what I mean. I can e-mail you the form I am working on (not quite sure how to upload) with the function that isn't suppose to work. But from my amateurish POV and lower intermediate skill set, it does work and it looks like it says
//if it ends in ".0" then remove the zeros following the second decimal point//
this is exactly what I put in the white space in VFE
{fn RemoveEndZero(var){
while sub(var,size(var),1) == ".0" do
var = sub(var,1,size(var) - 1)
endwhile
return var
}}{if DOCUMENT.VARIABLE1 <> "" then DOCUMENT.VARIABLE2 = RemoveEndZero(str(truncate(val(DOCUMENT.VARIABLE1),2))) else "" endif}
On the other hand, I agree, truncate and round should probably work just fine. Is it possible that my shoddy coding technique would void the function, truncate(value,2), and cause a value to = something like 1.08000 instead of 1.08?
Has anyone else tried the truncate functions?
In case anyone is interested, I figured out why the function seemed to work. A response from any of the more seasoned MEL coders is much appreciated, here.
the function doesn't work, like Michael said.
What seems to work is the following part
str(round(val(DOCUMENT.VARIABLE),2)) this returns a number and a decimal to the second position ex. 12.2265 is shown as 12.23
I guess that's how the round/truncate function example should look in the EMR...
unless you like the trailing zeros