bash - dc(1) and leading zeros -


I have a shell script and I do some calculations with DC (1).

I need a number printed with leading zero; I can not find an easy and straightforward way to do this with DC, but it is mentioned in the manpage:

Z stacks off values, calculates the number of digits (Or the number of characters, if this is a string) and pushing that number does not include any leading zeros in the number count for a number, even if they are on the right of the redx point Appear on the side.

What is the simplest and simplest way ...

I know that there is a mingle and one way to accomplish it, and I'm one of them I am walking with happiness. I'm just curious; -)

Give it a try:

Enter:

  [lc1 + dsc0nld & gt; B] SB [sddZdscld & gt; BP] Enter a 999 12lax   

Output:

  000000999   

:

  3lax   

Output:

  999   

After the macro ends, the original number is left on the stack Used Registers: a (macro), b (macro), c (count), d (Points).

Explanation:

sets the macro a , prints the call b and the original number. Points to copy in

  • sd - register d
  • dz - Save the number - Duplicate original number and count its digit count
  • DSC - calculate the duplicate and register it in the c
  • LD & gt; Load desired digits from d , if it is more than the count, then the macro b
  • p < / Code> - Print the original number

    The macro b output zero, unless the number of desired digits is not greater than

      < Li> LC1 + - Load c from the counting register and enlarge it
    • DSC - duplicate count and archive To register this, return < c
    • 0n - A newline output without a zero
    • ld> gt - the desired load register d , if it still increased If the count is higher then loop back to run the macro b again, otherwise this collar (macro a )

      To use arbitrary key character:

        [lclkZ + dsclknld & gt; B] SB [sksddZdscld & gt; BP] 99914 [] Lux 999 [ABC] 12 [-] Late --------- ABC   

      In addition to the other registers, Code> k uses the character (which can actually be more than one):

        [XYZ] 6 [DEF] waste dfx 8 [ab] lac ABBABX 4 G [ghjikl] lax ghijklXYZ   

      Fill strings are fully used, so the length of your expected length is greater than the length of the original string, but two strings The GSA (or is shorter than the length of the integer multiples), so the results may be longer than that.

Comments