April 29, 2024, 08:04:42 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Convert Number to String

Started by billhsln, November 22, 2009, 11:48:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

Here is a little some thing I created for those that might need it.

' Num2Text.EBA

def value as double
def ivalue as int
def ovalue as string
def rtn as string

' 999,999,999.99 max
value = 17285595.27

if value > 999999999.99
ovalue = "Value too large"
else
' determine Dollar value
ivalue = value
ovalue = ConvertNum2Text(ivalue) + "Dollars and "
' determine Cent value
value = value - ivalue
ivalue = value * 100 + 0.49
if ivalue = 0
ovalue += "No Cents"
else
ovalue += ConvertNum2Text(ivalue) + "Cents"
endif
print ovalue
endif

input rtn

end

sub ConvertNum2Text(i as int), string
def s as string
def o as string
def ti as int
def l as int

def cntr as int
def txtr as string

def n2s[27] as string

databegin NumData
data "One ","Two ","Three ","Four ","Five ","Six "
data "Seven ","Eight ","Nine ","Ten ","Eleven "
data "Twelve ","Thirteen ","Fourteen ","Fifteen "
data "Sixteen ","Seventeen ","Eighteen ","Nineteen "
data "Twenty ","Thirty ","Forty ","Fifty ","Sixty "
data "Seventy ","Eighty ","Ninety "
dataend

'Read NumData list into array
restore NumData     
for cntr = 0 to 26
getdata NumData,txtr
n2s[cntr] = txtr
next cntr

s = ltrim$(str$(i))
o = ""
do
l = len(s)
if l = 9 or l = 6 or l = 3
ti = val(left$(s,1))
if ti > 0 then o += n2s[ti-1] + "Hundred "
s = mid$(s,2)
else
if l = 8 or l = 5 or l = 2
ti = val(left$(s,2))
if ti > 0
if ti < 21
o += n2s[ti-1]
else
l = ti / 10 + 17
o += n2s[l]
i = ti - (ti / 10) * 10
if i > 0 then o += n2s[i-1]
endif
endif
s = mid$(s,3)
else
ti = val(left$(s,1))
if ti > 0 then o += n2s[ti-1]
s = mid$(s,2)
endif
endif
select len(s)
case 6
o += "Million "
case 3
o += "Thousand "
default
i = 0
endselect
until s = ""
return o
endsub


Enjoy,
Bill
When all else fails, get a bigger hammer.