I am using bash
I have a script that takes a number, i.e. 85.152, which is always a non integer and essentially tries to get that number to be a multiple of 10. My code is as follows:
number=85.152
A=${number%.*} #Converts float to integer
typeset -i B=$(((A-20)/10)) #subtracting 20 is desired, dividing by 10 is done to get the value less than 10 (only works for numbers less than 100)
typeset -i C=${B%.*} #converts float to integer (redundant I know, but necessary)
typeset -i answer=$((C * 10)) #Gives me a multiple of 10
Explanation:
I convert from a float to integer first because I cannot simply divide 85.152 by 10 using bash. It does not like dividing floats.
I subtract 20 because this is desired by design (it can be ignored). The division by 10 gets me to a number below 10 which is just a trick because any integer less than 10 which is multiplied by 10 will simply be a multiple of 10.
I convert from float to integer again just in case the number I divided, (to get less than 10), ends up dividing out to be another float (i.e. 85/10=8.5, then truncate to 8).
Finally I multiply by 10, 8*10=80 which is a multiple of 10.
I did what I could as a newbie to make it work but now I would like to simplify this process. I simply want to take 85.152 and make it a multiple of 10 that is 20 less than 85.152. So answer = (85.125-20) truncated to a multiple of 10. Should be 60. Any suggestions?
Last login: Sat Oct 24 08:38:01 on ttys000
AMIGA:barrywalker~> x=87.1173
AMIGA:barrywalker~> y=20
AMIGA:barrywalker~> echo $[${x%.*}-${x%.*}%10-$y]
60
AMIGA:barrywalker~> _
Note: the $[ ... ] is a non-standard, bash-specific legacy construct that has been deprecated since bash 2.0 and is now no longer in the man pages, so it should be regarded as an undocumented feature, which could be removed from future versions..
Instead, $(( .. )) should be used, which is also used in any other POSIX compliant shell.
I was/am aware, but the OP is using bash.
Sadly, I use(d) this method in AudioScope.sh from day one so that code is lumbered with this legacy.
However apart from my important note I did like RudiC's approach so I decided to reduce the number of characters, (ignoring spaces).
I will refrain from using this method again but will have to continue using it in the above project to keep the coding style the same...
Once again thanks and I will use the parentheses method in any future help...