Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting numbers to strings
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00172921
Message ID:
00172968
Views:
26
>Does anyone know the easiest way to convert a number to a string.
>
>IE 560 = Five Hundred Sixty

Here's a real old bit of code that still works....
Modify it to your own coding standards
parameter amt

* Convert a dollar amount to words
* e.g. 2135.56 -> 'Two thousand one hundred and thirty five dollars - 56›'
* Limit 999,999,999,999.99

private cStr, id, ic
ic= TRANSFORM(ROUND(100 * (m.amt % 1), 0), '@L 99') + 'c'		&& cents
id = int(m.amt)													&& dollars

if m.id > 999999
	cStr= Thousands(int(m.id/1000000.0000)) + ' million'
else
	cStr= ''
endif
id= m.id % 1000000
if m.id > 0
	cStr= iif(empty(m.cStr), '', m.cStr + ' ') + Thousands(m.id)
endif
if empty(m.cStr)
	return m.ic
else
	return UPPER(LEFT(m.cStr, 1)) + SUBSTR(m.cStr, 2) ;
		+ ' dollar' + iif(m.amt < 2, '', 's') + '-' + m.ic
endif

Function Thousands
parameter id
* how many thousands
private cStr
if m.id > 999
	cStr= Hundreds(int(m.id/1000.00)) + ' thousand'
else
	cStr= ''
endif
id= m.id % 1000
if m.id > 0
	cStr= iif(empty(m.cStr), '', m.cStr + ' ') ;
		+ iif(int(m.id/100)=0 and ! empty(m.cStr), 'and ', '') ;
		+ Hundreds(m.id)
endif
return m.cStr

function Hundreds
parameter id
private cStr
if m.id > 99
	cStr= Tens(int(m.id/100.0)) + ' hundred'
else
	cStr= ''
endif
id= m.id % 100
if m.id > 0
	cStr= iif(empty(m.cStr), '', m.cStr + ' and ') + Tens(m.id)
endif
return m.cStr

function Tens
parameter id
private cStr
cStr= ''
do case
case m.id > 89
	cStr= 'ninety'
case m.id > 79
	cStr= 'eighty'
case m.id > 69
	cStr= 'seventy'
case m.id > 59
	cStr= 'sixty'
case m.id > 49
	cStr= 'fifty'
case m.id > 39
	cStr= 'forty'
case m.id > 29
	cStr= 'thirty'
case m.id > 19
	cStr= 'twenty'
otherwise
	cStr= Units(m.id)
endcase

if m.id > 20
	id= m.id % 10
	cStr= m.cStr + ' ' + units(m.id)
endif
return m.cStr

procedure Units
parameter id
* handles 1 to 15
do case
case m.id = 0
	return ''
case m.id = 1
	return 'one'
case m.id = 2
	return 'two'
case m.id = 3
	return 'three'
case m.id = 4
	return 'four'
case m.id = 5
	return 'five'
case m.id = 6
	return 'six'
case m.id = 7
	return 'seven'
case m.id = 8
	return 'eight'
case m.id = 9
	return 'nine'
case m.id = 10
	return 'ten'
case m.id = 11
	return 'eleven'
case m.id = 12
	return 'twelve'
case m.id = 13
	return 'thirteen'
case m.id = 14
	return 'fourteen'
case m.id = 15
	return 'fifteen'
case m.id = 16
	return 'sixteen'
case m.id = 17
	return 'seventeen'
case m.id = 18
	return 'eighteen'
case m.id = 19
	return 'nineteen'
endcase
Previous
Reply
Map
View

Click here to load this message in the networking platform