I have a number, say 174. I need to write bash code that will find the first larger number that ends in 99. That would be 199 in this case. If the number were 1263, I would be looking for 1299, for 175438, I would want 175499, etc.
If the numbers were always three digit, I could just grab the first digit and add 99. NEW_NUMBER=${OLD_NUMBER:0:1}'99'
I guess what I would want to do is to just remove the last two chars and replace with 99. NEW_NUMBER=${OLD_NUMBER:0:-2}'99'
A minor nitpick - suppose your number is 123499. wisecrackers code will return 123499. Is that the NEXT larger number ending in 99, after 123499? You get to decide.
This kind of thing is sometimes called an edge condition.
Sorry for the delay, I went to the store and came back to find allot of very nice posts. After thinking about it, if the first number happens to end in *99,
FIRST_NUMBER='199'
then I would want 299 for my second number and not a repeat of 199.
So I guess this code will do what I need,
SECOND_NUMBER=$((((FIRST_NUMBER+1)/100+1)*100-1))
I plugged this into my script and I am getting the behavior I expect.
Is there some reason why my thought of just replacing the last two chars is ill conceived? The code I posted didn't work and I was getting a substring expression < 0 error.
I guess what I would have done here would have been to test $FIRST_NUMBER,
if [ "$FIRST_NUMBER" == "$SECOND_NUMBER" ]; then
let "SECOND_NUMBER=$FIRST_NUMBER+100"
fi
to make sure I didn't end up in the same place, but it is nice to do this in one step.
All of the responses so far are assuming that the OLD_NUMBER is non-negative. If OLD_NUMBER can be less than -99, it is a little more complicated, but I think this works:
for OLD_NUMBER in "$@"
do
NEW_NUMBER=$((((OLD_NUMBER<=-100)*(OLD_NUMBER/100*100+1))+((OLD_NUMBER>-100)*((((OLD_NUMBER+1)/100)+1)*100-1))))
printf '%s -> %d\n' "$OLD_NUMBER" "$NEW_NUMBER"
done