Howdy Fellow CHUGgers!
Looking to find the syntax or verbiage for limiting a numerical result to x number of decimal places. Let's say, two for this example (x.xx).
Bonus Points: Linking to an online information source for this kind knowledge (0 points awarded for linking to CHUG forums). 😉
Thanks!
Would you like to round your result? If so there is a built in function for that. From CPS help:
round
description Rounds a value to the specified decimal point. type MEL utility function syntax round(value, point) arguments
example 1 {fn RND_HT( ){ //define function local hgt = val(OBSANY("Height")) round(hgt,0)}} example 2 {RND_HT( )} //call function Returns 76 where OBSANY(’Height’) returns 75.75 The function defined in this example uses round() to return a rounded height value. |
You can also use truncate is you don't want to round your value. Also from CPS help:
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.
bhoover for the win. + bonus bonus points. 🙂
THANKS!
I have the below code, but it will not round or limit the results to the two decimal places as I want. It goes out to the 6th decimal place.
//Weight Conversion to KG
{fn RND_WT(val1){//rounds KG to 2 decimal places
local wt = (val1 * 0.45)
round(wt,2)}}
{!DOCUMENT.PT_WEIGHT_KG = RND_WT(DOCUMENT.PT_WEIGHT)}
It may be that your function is not returning a value. try this:
//Weight Conversion to KG
{fn RND_WT(val1){//rounds KG to 2 decimal places
local wt = (val1 * 0.45)
return round(wt,2)}}
{!DOCUMENT.PT_WEIGHT_KG = RND_WT(DOCUMENT.PT_WEIGHT)}