ios - Shortest way to get digit number from a value -


let's have number 134658 , want 3rd digit (hundreds place) "6".

what's shortest length code in objective-c?

this current code:

int thenumber = 204398234; int thedigitplace = 3;//hundreds place int thedigit = (int)floorf((float)((10)*((((float)thenumber)/(pow(10, thedigitplace)))-(floorf(((float)thenumber)/(pow(10, thedigitplace)))))));  //returns "2" 

there better solutions, 1 shorter:

int thenumber = 204398234; int thedigitplace = 3;//hundreds place int thedigit = (thenumber/(int)(pow(10, thedigitplace - 1))) % 10; 

in case, divides number 100 2043982 , "extracts" last decimal digit "remainder operator" %.

remark: solution assumes result of pow(10, thedigitplace - 1) exact. works because double has 16 significant decimal digits , int on ios 32-bit number , has @ 10 decimal digits.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -