|
Home > Archive > Unix Shell > November 2006 > padding integers in bc
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
padding integers in bc
|
|
| Robert Katz 2006-11-27, 1:29 am |
| I'm trying to define a bc function that will pad an integer with
leading 0s.
For example, I want pad(57,4) -> 0057; pad(593,2) -> 593
This definition of pad puts the correct number of leading 0s, but it
also adds a trailing 0. I don't know why.
####
define pad(x,y){
auto w
w=y-length(x)
while(w>0){print"0";w-=1}
print x
}
####
--
Regards,
---Robert
| |
| loic-dev@gmx.net 2006-11-27, 1:29 am |
| Hello Robert,
> I'm trying to define a bc function that will pad an integer with
> leading 0s.
>
> For example, I want pad(57,4) -> 0057; pad(593,2) -> 593
>
> This definition of pad puts the correct number of leading 0s, but it
> also adds a trailing 0. I don't know why.
>
> ####
> define pad(x,y){
> auto w
> w=y-length(x)
> while(w>0){print"0";w-=1}
> print x
> }
> ####
The trailing '0' you are seeing is in fact the return value of the
pad() function. Doing something like:
ret = pad(57,4)
should produce the 'correct' output.
HTH,
Loic.
| |
| Robert Katz 2006-11-27, 1:18 pm |
| loic-dev@gmx.net wrote:
> Hello Robert,
>
>
> The trailing '0' you are seeing is in fact the return value of the
> pad() function. Doing something like:
> ret = pad(57,4)
>
> should produce the 'correct' output.
>
> HTH,
> Loic.
>
This worked:
define pad(x,y){
auto w
w=y-length(x)
while(w>0)print"0";w-=1}
return x
}
--
Regards,
---Robert
| |
| Janis Papanagnou 2006-11-27, 1:18 pm |
| Robert Katz wrote:
> loic-dev@gmx.net wrote:
>
>
> This worked:
Not quite.
>
> define pad(x,y){
> auto w
> w=y-length(x)
> while(w>0)print"0";w-=1}
while(w>0){print"0";w-=1}
> return x
> }
>
Or using... while(w-->0)print"0"
Janis
| |
| Robert Katz 2006-11-27, 7:23 pm |
| Janis Papanagnou wrote:
> Robert Katz wrote:
>
> Not quite.
Sure it did. See my previous message, and you'll see that the 'This
worked:' comment refers to the next routine, not the previous one,
which worked ... well, Not quite.
>
>
> while(w>0){print"0";w-=1}
>
>
> Or using... while(w-->0)print"0"
>
--
Regards,
---Robert
| |
| Janis Papanagnou 2006-11-27, 7:23 pm |
| Robert Katz wrote:
> Janis Papanagnou wrote:
>
>
>
> Sure it did.
The routine you gave below gives a syntax error because you have
omitted an opening curly brace after while(w>0). I've fixed your
error in my reply (as you can see by the indentation level) below.
> See my previous message, and you'll see that the 'This
> worked:' comment refers to the next routine, not the previous one, which
> worked ... well, Not quite.
>
>
>
| |
| Robert Katz 2006-11-28, 1:32 am |
| Janis Papanagnou wrote:
> Robert Katz wrote:
>
> The routine you gave below gives a syntax error because you have
> omitted an opening curly brace after while(w>0). I've fixed your
> error in my reply (as you can see by the indentation level) below.
Dang it! You got me there. Not only would it not quite work, it
wouldn't work at all.[vbcol=seagreen]
>
--
Regards,
---Robert
|
|
|
|
|