Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine place values for any base

Tags:

math

I'm looking for a function that will determine the value of a place given a number and a base. For example,

Given:

Whole Value: 1120
Base: 10
Place: Tens place

Should return: 2

Does anybody know the math for this?

Edit: The function is also expected to pass the whole value numerically, not as a string like "e328fa" or something. Also the return value should be numeric as well, so a FindInPlace(60 (whole value), 16 (base), 2 (place, 1-based index)) should return 3.

like image 387
Dan Avatar asked Dec 30 '22 04:12

Dan


1 Answers

With 1-based place indexing the formula is:

placeval = floor(number / (base^(place-1))) mod base

In Python:

def FindInPlace(number, base, place):
    return number//base**(place-1) % base
like image 148
Theran Avatar answered Jan 23 '23 04:01

Theran