Maple: use one digit of a long number

124 Views Asked by At

I'm writing a procedure in Maple and was wondering if there was a way to pick out one digit of a number - for example, if the number was $18475$ and I wanted the tens digit I would do something to $18475$ to be able to use just the number 7.

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

One way to get the digit corresponding to a multiple of $10^i$ (that is, the i-th digit from the right when counting the ones place as the $0$-th digit) is to divide by $10^i$, floor the result and then mod 10 it. So, if i want the 10s place (corresponding to $i=1$), $18475\div 10^1 = 1847.5$, flooring it gives 1847. mod 10'ing it gives 7. (This assumes the number is positive - if it is negative, multiply by -1 first)

You can also convert the number to a string and then extract the relevant digit by which character it is in the string.