APSPFNC7 ;IHS/MSC/PJJ/PLS - Number to Word Formatting ;25-Feb-2013 11:56;DKA
;;7.0;IHS PHARMACY MODIFICATIONS;**1015**;Sep 23, 2004;Build 62
;=================================================================
;Returns textual representation of a numeric value
WRDFMT(INT) ;EP-
N RET,COMP,X
S RET=""
Q:INT'=+INT RET
; DKA 2013-02-25 artf13536 Return null for decimal values, per grammatical standard.
Q:INT'=(INT\1) RET
S X=+INT
Q:+INT=0 $$LABEL($T(BASE+1))
F I=1:1:3 Q:X=0 D
.S COMP(I)=X#1000
.S X=X\1000
F I=1:1:3 D
.Q:'$G(COMP(4-I))
.S RET=$$ADD(RET,$$ADD($$GRPTOWD(COMP(4-I)),$$LABEL($T(SCALES+4-I))),", ")
K COMP,X
Q RET
;
LABEL(TEXT) ;EP-
;Q $E(TEXT,4,99)
Q $P(TEXT,";;",2)
ADD(RES,EXTRA,SEP) ;EP-
Q RES_$S((RES'="")&(EXTRA'=""):$G(SEP," "),1:"")_EXTRA
GRPTOWD(GROUP) ;EP-
N HDRDS,TENMOD,TENS,ONES,RES
S RES=""
S HDRDS=GROUP\100
S TENMOD=GROUP#100
S TENS=TENMOD\10
S ONES=TENMOD#10
S:HDRDS'=0 RES=$$LABEL($T(BASE+HDRDS+1))_" Hundred"
I TENS>1 D
.S RES=$$ADD(RES,$$LABEL($T(TENS+TENS+1)))
.S:ONES'=0 RES=$$ADD(RES,$$LABEL($T(BASE+ONES+1)))
E I TENMOD'=0 D
.S RES=$$ADD(RES,$$LABEL($T(BASE+TENMOD+1)))
Q RES
BASE ;EP-
;;Zero
;;One
;;Two
;;Three
;;Four
;;Five
;;Six
;;Seven
;;Eight
;;Nine
;;Ten
;;Eleven
;;Twelve
;;Thirteen
;;Fourteen
;;Fifteen
;;Sixteen
;;Seventeen
;;Eighteen
;;Nineteen
TENS ;EP-
;
;
;;Twenty
;;Thirty
;;Forty
;;Fifty
;;Sixty
;;Seventy
;;Eighty
;;Ninety
SCALES ;EP-
;
;;Thousand
;;Million
;;Billion
;;Trillion
ASSERT(EXPR,MSG) ;EP-
N Y
X EXPR
W $S(Y:"Passed",1:"Failed")_": "_MSG,!
Q Y
ASSRTEQ(EXP,ACTL) ;EP-
Q $$ASSERT("S Y=$S("""_EXP_"""="""_ACTL_""":1,1:0)",EXP_" equal to "_ACTL)
TEST ;EP-
Q:'$$ASSRTEQ($$WRDFMT(0),"Zero")
Q:'$$ASSRTEQ($$WRDFMT(60),"Sixty")
Q:'$$ASSRTEQ($$WRDFMT(1),"One")
Q:'$$ASSRTEQ($$WRDFMT(17),"Seventeen")
Q:'$$ASSRTEQ($$WRDFMT(100),"One Hundred")
Q:'$$ASSRTEQ($$WRDFMT(150),"One Hundred Fifty")
Q:'$$ASSRTEQ($$WRDFMT(115),"One Hundred Fifteen")
Q:'$$ASSRTEQ($$WRDFMT(1115),"One Thousand, One Hundred Fifteen")
Q:'$$ASSRTEQ($$WRDFMT(2000114),"Two Million, One Hundred Fourteen")
Q:'$$ASSRTEQ($$WRDFMT(6.5),"")
APSPFNC7 ;IHS/MSC/PJJ/PLS - Number to Word Formatting ;25-Feb-2013 11:56;DKA
+1 ;;7.0;IHS PHARMACY MODIFICATIONS;**1015**;Sep 23, 2004;Build 62
+2 ;=================================================================
+3 ;Returns textual representation of a numeric value
WRDFMT(INT) ;EP-
+1 NEW RET,COMP,X
+2 SET RET=""
+3 IF INT'=+INT
QUIT RET
+4 ; DKA 2013-02-25 artf13536 Return null for decimal values, per grammatical standard.
+5 IF INT'=(INT\1)
QUIT RET
+6 SET X=+INT
+7 IF +INT=0
QUIT $$LABEL($TEXT(BASE+1))
+8 FOR I=1:1:3
IF X=0
QUIT
Begin DoDot:1
+9 SET COMP(I)=X#1000
+10 SET X=X\1000
End DoDot:1
+11 FOR I=1:1:3
Begin DoDot:1
+12 IF '$GET(COMP(4-I))
QUIT
+13 SET RET=$$ADD(RET,$$ADD($$GRPTOWD(COMP(4-I)),$$LABEL($TEXT(SCALES+4-I))),", ")
End DoDot:1
+14 KILL COMP,X
+15 QUIT RET
+16 ;
LABEL(TEXT) ;EP-
+1 ;Q $E(TEXT,4,99)
+2 QUIT $PIECE(TEXT,";;",2)
ADD(RES,EXTRA,SEP) ;EP-
+1 QUIT RES_$SELECT((RES'="")&(EXTRA'=""):$GET(SEP," "),1:"")_EXTRA
GRPTOWD(GROUP) ;EP-
+1 NEW HDRDS,TENMOD,TENS,ONES,RES
+2 SET RES=""
+3 SET HDRDS=GROUP\100
+4 SET TENMOD=GROUP#100
+5 SET TENS=TENMOD\10
+6 SET ONES=TENMOD#10
+7 IF HDRDS'=0
SET RES=$$LABEL($TEXT(BASE+HDRDS+1))_" Hundred"
+8 IF TENS>1
Begin DoDot:1
+9 SET RES=$$ADD(RES,$$LABEL($TEXT(TENS+TENS+1)))
+10 IF ONES'=0
SET RES=$$ADD(RES,$$LABEL($TEXT(BASE+ONES+1)))
End DoDot:1
+11 IF '$TEST
IF TENMOD'=0
Begin DoDot:1
+12 SET RES=$$ADD(RES,$$LABEL($TEXT(BASE+TENMOD+1)))
End DoDot:1
+13 QUIT RES
BASE ;EP-
+1 ;;Zero
+2 ;;One
+3 ;;Two
+4 ;;Three
+5 ;;Four
+6 ;;Five
+7 ;;Six
+8 ;;Seven
+9 ;;Eight
+10 ;;Nine
+11 ;;Ten
+12 ;;Eleven
+13 ;;Twelve
+14 ;;Thirteen
+15 ;;Fourteen
+16 ;;Fifteen
+17 ;;Sixteen
+18 ;;Seventeen
+19 ;;Eighteen
+20 ;;Nineteen
TENS ;EP-
+1 ;
+2 ;
+3 ;;Twenty
+4 ;;Thirty
+5 ;;Forty
+6 ;;Fifty
+7 ;;Sixty
+8 ;;Seventy
+9 ;;Eighty
+10 ;;Ninety
SCALES ;EP-
+1 ;
+2 ;;Thousand
+3 ;;Million
+4 ;;Billion
+5 ;;Trillion
ASSERT(EXPR,MSG) ;EP-
+1 NEW Y
+2 XECUTE EXPR
+3 WRITE $SELECT(Y:"Passed",1:"Failed")_": "_MSG,!
+4 QUIT Y
ASSRTEQ(EXP,ACTL) ;EP-
+1 QUIT $$ASSERT("S Y=$S("""_EXP_"""="""_ACTL_""":1,1:0)",EXP_" equal to "_ACTL)
TEST ;EP-
+1 IF '$$ASSRTEQ($$WRDFMT(0),"Zero")
QUIT
+2 IF '$$ASSRTEQ($$WRDFMT(60),"Sixty")
QUIT
+3 IF '$$ASSRTEQ($$WRDFMT(1),"One")
QUIT
+4 IF '$$ASSRTEQ($$WRDFMT(17),"Seventeen")
QUIT
+5 IF '$$ASSRTEQ($$WRDFMT(100),"One Hundred")
QUIT
+6 IF '$$ASSRTEQ($$WRDFMT(150),"One Hundred Fifty")
QUIT
+7 IF '$$ASSRTEQ($$WRDFMT(115),"One Hundred Fifteen")
QUIT
+8 IF '$$ASSRTEQ($$WRDFMT(1115),"One Thousand, One Hundred Fifteen")
QUIT
+9 IF '$$ASSRTEQ($$WRDFMT(2000114),"Two Million, One Hundred Fourteen")
QUIT
+10 IF '$$ASSRTEQ($$WRDFMT(6.5),"")
QUIT