
The total field length may be entered, or calculated (read comments) and the number of decimal positions. Thousands are separated
Code: Select all
' testprogram for n2a$() function
'
for i=1 to 9 ! read num(i) ! next i
data 87643210.23
data 6.432123E+4
data 0.00123456
data 12345678.123456
data -12345678.12
data 2000
data 13.5E+8
data -.003678E+1
data 13.5E-9
for i=1 to 8
print n2a$(num(i),0,4)&" ";num(i)
next i
print
for i=1 to 8
print n2a$(num(i),20,4)&" ";num(i)
next i
end
' format a number for PRINT or DRAW TEXT statement
' num = the number to be formatted
' len = the field length to be used for the formatted number,
' inclusive decimals, sign, dots, and comma
' len=0 will automatically use the shortest field needed
' dec = the number of decimal positions
' to print a table right-aligned, give len a value of at least the
' longest number in the table. Giving even more than that, will
' have a TAB effect for the whole table
'
def n2a$(num,len,dec)
fs$=" " ! fh$="################" ! s=0
th$="," ! dec$="." ' uk/us tokens for thousands and decimal
if num<0 then ! s=1 ! num=-num ! else ! s=0 ! end if
ent=floor(num) ! frac=fract(num) ! spaces=0
pre=max(floor(log10(num)+1),1) ! noc=floor((pre-1)/3)
le=pre+noc+s ! if dec then le+=dec+1
if len and len>le then spaces=len-le
if spaces then format$=left$(fs$,spaces) else format$=""
if s then format$ &= "-"
format$ &= left$(fh$,pre-3*noc)
if noc then
for i=1 to noc ! format$ &= th$ & "###" ! next i
end if
if dec then format$ &= dec$ & left$(fh$,dec)
return str$(num,format$)
end def