/
ntow.prg
58 lines (43 loc) · 1.78 KB
/
ntow.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* This is an original work by Gary Baren and is placed in the public domain.
Rev 1.1 15 Aug 1991 23:05:54 GLENN
Forest Belt proofread/edited/cleaned up doc
Rev 1.0 09 Jun 1991 00:26:56 GLENN
Initial revision.
*/
FUNCTION ft_NToW( nAmount )
STATIC sc_qualifiers := { "", " Thousand", " Million", " Billion", " Trillion" }
LOCAL nTemp, cResult
LOCAL nDiv := 10 ^ ( Int( sol10( nAmount := Max( 0, nAmount ) ) / 3 ) * 3 )
nTemp := Int( nAmount % nDiv )
nAmount := Int( nAmount / nDiv )
cResult := grp_to_words( nAmount ) + ;
sc_qualifiers[ Int( sol10( nDiv ) / 3 ) + 1 ]
IF nTemp > ( nDiv /= 1000 ) .AND. nDiv > 1
cResult += " " + ft_NToW( nTemp )
ELSE
cResult += grp_to_words( nTemp )
ENDIF
RETURN LTrim( cResult )
STATIC FUNCTION grp_to_words( nGrp )
STATIC sc_ones := { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }
STATIC sc_teens := { " Ten", " Eleven", " Twelve", ;
" Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" }
STATIC sc_tens := { "", "", ;
" Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" }
LOCAL cResult, nTemp
nTemp := Int( nGrp % 100 )
nGrp := Int( nGrp / 100 )
cResult := iif( nGrp > 0, " " + sc_ones[ nGrp ] + " Hundred", "" )
DO CASE
CASE nTemp > 19
cResult += ;
sc_tens[ Int( nTemp / 10 ) + 1 ] + ;
iif( ( nTemp := Int( nTemp % 10 ) ) > 0, "-" + sc_ones[ nTemp ], "" )
CASE nTemp < 20 .AND. nTemp > 9
cResult += sc_teens[ Int( nTemp % 10 ) + 1 ]
CASE nTemp < 10 .AND. nTemp > 0
cResult += " " + sc_ones[ nTemp ]
ENDCASE
RETURN cResult
STATIC FUNCTION sol10( nNumber )
RETURN Len( hb_ntos( Int( nNumber ) ) ) - 1