Custom USING function (thousand separators)
Jump to navigation
Jump to search
Description
- user-defined function
function using$(mask$, x)
- allows to add thousends separator, by including them im the mask.
- "###,###,###.##"
- Actually anything between # gets added to output, provided there are enough digits counting from the right.
- (else it puts space instead, so whole length stays the same as mask)
So it can do
- "### ### ###.##"
- "x=###,###,###.##$"
and even
- "(###)##-##-###"
- If you don't need extra spaces from the left, use TRIM$() like this
- trim$(using$(mask$, x))
randomize .5
print "String";tab(15);"Val";tab(30);"Using";tab(50);"using$"
for i = 1 to 20
n = int(rnd(1)*10)
a$=""
for j = 1 to n
a$=a$;int(rnd(1)*10)
next
a$=a$;"."
for j = 1 to 3
a$=a$;int(rnd(1)*10)
next
if rnd(1)<0.5 then a$="-";a$
a=val(a$)
print a$;tab(15);a;tab(30);using("############.##", a);tab(50);using$("###,###,###,###.##", a)
next
print
print "Also can do:"
x=1234567.8901
mask$ = "###,###,###.##"
print x, ">";mask$;"<", ">";using$(mask$, x);"<"
mask$ = "x=###,###,###.##$"
print x, ">";mask$;"<", ">";using$(mask$, x);"<"
mask$ = "any text ###,###,###.## more"
print x, ">";mask$;"<", ">";using$(mask$, x);"<"
mask$ = "### ###,###.##"
print x, ">";mask$;"<", ">";using$(mask$, x);"<"
mask$ = "### ### ###.##"
print x, ">";mask$;"<", ">";using$(mask$, x);"<"
function using$(mask$, x)
delim1000$ = "," 'the point is, we do not print them (print " ") if there are no digit in this place
realMask$ = ""
for i = 1 to len(mask$)
c$=mid$(mask$,i,1)
if instr("#.", c$) then realMask$ = realMask$ + c$
next
x$=using(realMask$, x)
'print x$
xx$=""
j = 1
digitStarted=0
for i = 1 to len(mask$)
c$=mid$(mask$,i,1)
if instr("#.", c$) then
cc$=mid$(x$,j,1)
xx$ = xx$ + cc$
j=j+1
digitStarted = (instr(" -", cc$)=0)
else
if instr(delim1000$, c$) then
if digitStarted = 0 then
if cc$="-" then 'if we have " -," we should convert it to " -"
xx$ = left$(xx$, len(xx$)-1) + " "
c$="-"
else
c$=" " 'skip thousand delimiter if digits not started yet
end if
end if
end if
xx$ = xx$ + c$
end if
next
using$ = xx$
end function