RosettaCodeData/Task/Formatted-numeric-output/Liberty-BASIC/formatted-numeric-output.liberty
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

20 lines
678 B
Text

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