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.




 
{def fmt
 {def padd {lambda {:n :x} {if {< :n 1} then else :x{padd {- :n 1} :x}}}}
 {def trunc {lambda {:n} {if {> :n 0} then {floor :n} else {ceil :n}}}} 
 {lambda {:a :b :n} 
  {let { {:a :a} {:b :b} {:n {abs :n}} {:sign {if {>= :n 0} then + else -}}
         {:int {trunc :n}}
         {:dec {ceil {* 1.0e:b {abs {- :n {trunc :n}}}}} }          
       } {br}{padd {- :a {W.length {trunc :n}}} >}  
         {if {W.equal? :sign -} then else :sign}:int.:dec{padd {- :b {W.length :dec}} 0} }}}
-> fmt
 
{def numbers  
     7.125
     10.7
     0.980
     -1000
     559.8
     -69.99
     4970.430}               
-> numbers
 
{S.map {fmt 10 3} {numbers}}
-> 
>>>>>>>>> +7.125 
>>>>>>>> +10.699 
>>>>>>>>> +0.980 
>>>>>> -1000.000 
>>>>>>> +559.799 
>>>>>>>> -69.990 
>>>>>> +4970.430
 