Task

Express a number in decimal as a fixed-length string with leading zeros.



For example, the number   7.125   could be expressed as   00007.125.




 
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
 