An Action Block to left and/or right pad any number

Dm114

Well-known member
This Action Block is made to pad any integer or decimal number with any single character on the left side of the integer part. The decimal part (if any) can also be padded with zeros (0) on the right side.

INPUT variables:
• Number: string variable containing the number to be padded
• PaddingChar: single character used to pad on the left side (leading padding). Default: 0 (zero)
• LengthLeading: number of characters to pad on the left side. 2≤LengthLeading≤20 (default: 2)
• LengthTrailing: number of zeros (0) to pad on the right side of a decimal number. 0≤LengthTrailing≤20 (default: 0)

OUTPUT variable:
• PaddedNumber: returns an empty string if Number is not a regular number.

💡 If PaddingChar is 0 (zero), the minus sigh (-) of negative numbers remains on the left side. Otherwise (non-zero padding character), minus sign (-) will be added after the rightmost position and a space will take place of the minus sign (-) for positive numbers.


Enjoy!
 

Qarboz

Well-known member
This Action Block is made to pad any integer or decimal number with any single character on the left side of the integer part. The decimal part (if any) can also be padded with zeros (0) on the right side.

INPUT variables:
• Number: string variable containing the number to be padded
• PaddingChar: single character used to pad on the left side (leading padding). Default: 0 (zero)
• LengthLeading: number of characters to pad on the left side. 2≤LengthLeading≤20 (default: 2)
• LengthTrailing: number of zeros (0) to pad on the right side of a decimal number. 0≤LengthTrailing≤20 (default: 0)

OUTPUT variable:
• PaddedNumber: returns an empty string if Number is not a regular number.

💡 If PaddingChar is 0 (zero), the minus sigh (-) of negative numbers remains on the left side. Otherwise (non-zero padding character), minus sign (-) will be added after the rightmost position and a space will take place of the minus sign (-) for positive numbers.


Enjoy!
Good work ;)
I have a doubt about LengthLeading, though. To me it looks like the minimum length of the output string. I tried with Number=5 (integer) and LengthLeading=2 and the output is 05. But if Number=46 or 567 the output is 46 or 567.
Or I misunderstood the purpose of the action block.
 

Dm114

Well-known member
Good work ;)
I have a doubt about LengthLeading, though. To me it looks like the minimum length of the output string. I tried with Number=5 (integer) and LengthLeading=2 and the output is 05. But if Number=46 or 567 the output is 46 or 567.
Or I misunderstood the purpose of the action block.
Thanks.

That's normal: as padding is set to 2 and both numbers are 2 or 3 digits long, it can't be padded to a shorter length.

What output did you expect in this case?
 

Qarboz

Well-known member
Thanks.

That's normal: as padding is set to 2 and both numbers are 2 or 3 digits long, it can't be padded to a shorter length.

What output did you expect in this case?
I had understood that the action block is for adding n characters before and after the number, probably because I misinterpreted the meaning of the LengthLeading input.
Actually the action block is very useful as you made it, e.g., to write date and time in dd-mm-yyyy hh:mm format even if the day/month/time/minute is only one digit ;)
 

Dm114

Well-known member
I had understood that the action block is for adding n characters before and after the number, probably because I misinterpreted the meaning of the LengthLeading input.
Actually the action block is very useful as you made it, e.g., to write date and time in dd-mm-yyyy hh:mm format even if the day/month/time/minute is only one digit ;)
It's exactly one of the cases where it could be useful. Also to display a list of figures (like in bookkeeping) of the same total length (with leading zeros or asterisk * for instance).

But I must admit this Action Block is not the most powerful and useful, except in some cases! 😀

I made it for one personal specific use and also for the fun: MD is so pleasant to use! 😉
 

DonDemon

Active member
This Action Block is made to pad any integer or decimal number with any single character on the left side of the integer part. The decimal part (if any) can also be padded with zeros (0) on the right side.

INPUT variables:
• Number: string variable containing the number to be padded
• PaddingChar: single character used to pad on the left side (leading padding). Default: 0 (zero)
• LengthLeading: number of characters to pad on the left side. 2≤LengthLeading≤20 (default: 2)
• LengthTrailing: number of zeros (0) to pad on the right side of a decimal number. 0≤LengthTrailing≤20 (default: 0)

OUTPUT variable:
• PaddedNumber: returns an empty string if Number is not a regular number.

💡 If PaddingChar is 0 (zero), the minus sigh (-) of negative numbers remains on the left side. Otherwise (non-zero padding character), minus sign (-) will be added after the rightmost position and a space will take place of the minus sign (-) for positive numbers.


Enjoy!
Action block is adding one number on the left or right side?
 

Dm114

Well-known member
Action block is adding one number on the left or right side?
Not exactly.

On the right side of a decimal number it adds as many zeros (0) to get the total length of decimal digits as specified in LengthTrailing.

On the left side, any kind of number can be padded with any single character to the total length of digits specified by LengthLeading. The most common (and logical) characters are zero (0), asterisk (*) or underscore (_), but you can choose the one you want to.
 
Last edited:

Dm114

Well-known member
Not exactly.

On the right side of a decimal number it adds as many zeros (0) to get the total length of decimal digits as specified in LengthTrailing.

On the left side, any kind of number can be padded with any single character to the total length of digits specified by LengthLeading. The most common (and logical) characters are zero (0), asterisk (*) or underscore (_), but you can choose the one you want to.
Of course, when the original number of digits exceeds the LengthLeading and/or LengthTrailing (i.e. left integer part and/or right decimal part), original Number is not truncated and each concerned part is retrieved as is.

💡 It's up to you to choose parameters value in accordance with the maximum length of the figures you plan to work with.
 
Top