RosettaCodeData/Task/Formatted-numeric-output/Liberty-BASIC/formatted-numeric-output.basic

21 lines
693 B
Plaintext

for i = 1 to 10
n = rnd(1) * 10 ^ (int(10 * rnd(1)) - 2)
print "Raw number = "; n, "Using custom function = "; FormattedPrint$(n, 16, 5)
next i
end
function FormattedPrint$(n, length, decPlaces)
format$ = "#."
for i = 1 to decPlaces
format$ = format$ + "#"
next i
n$ = using(format$, n) ' remove leading spaces if less than 3 figs left of decimal
' add leading zeros
for i = 1 to len(n$)
c$ = mid$(n$, i, 1)
if c$ = " " or c$ = "%" then nn$ = nn$ + "0" else nn$ = nn$ + c$
next i
FormattedPrint$ = right$( "000000000000" +nn$, length) ' chop to required length
end function