Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #14320 > unrolled thread
| Started by | Pierre Gaston <pierre.gaston@gmail.com> |
|---|---|
| First post | 2018-07-10 19:16 +0300 |
| Last post | 2018-07-10 19:16 +0300 |
| Articles | 1 — 1 participant |
Back to article view | Back to gnu.bash.bug
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Number with sign is read as octal despite a leading 10# Pierre Gaston <pierre.gaston@gmail.com> - 2018-07-10 19:16 +0300
| From | Pierre Gaston <pierre.gaston@gmail.com> |
|---|---|
| Date | 2018-07-10 19:16 +0300 |
| Subject | Re: Number with sign is read as octal despite a leading 10# |
| Message-ID | <mailman.3386.1531239420.1292.bug-bash@gnu.org> |
On Tue, Jul 10, 2018 at 1:44 PM, Ilkka Virta <itvirta@iki.fi> wrote:
> I think the problematic case here is when the number comes as input from
> some program, which might or might not print a leading sign or leading
> zeroes, but when we know that the number is, in any case, decimal.
>
> E.g. 'date' prints leading zeroes, which is easy enough to handle:
>
> hour=$(date +%H)
>
> hour=${hour#0} # remove one leading zero, or
> hour="10#$hour" # make it base-10
>
> The latter works even with more than one leading zero, but neither works
> with a sign. So, handling numbers like '-00159' gets a bit annoying:
>
> $ num='-00159'
> $ num="${num:0:1}10#${num:1}"; echo $(( num + 1 ))
> -158
>
> And that's without checking that the sign was there in the first place.
>
>
> Something like that will probably not be too common, but an easier way to
> force any number to be interpreted in base-10 (regardless of leading
> zeroes) could be useful. If there is a way, I'd be happy to hear.
It's not too complicated to separate the sign from the number eg:
for num in 159 000159 +000159 -000159;do
echo $((${num%%[!+-]*}10#${num#[-+]}))
done
Back to top | Article view | gnu.bash.bug
csiph-web