Search code examples
mathfloating-pointintegerfloating-point-conversioninteger-arithmetic

Finding the tenth's place equivalent of an integer


How does one, computationally and dynamically, derive the 'ths' place equivalent of a whole integer? e.g.:

  • 187 as 0.187
  • 16 as 0.16
  • 900041 as 0.900041

I understand one needs to compute the exact th's place. I know one trick is to turn the integer into a string, count how many places there are (by how many individual characters there are) and then create our future value to multiply against by the tenth's value derived - like how we would on pen and paper - such as:

char integerStr[7] = "186907";
int strLength = strlen(integerStr);
double thsPlace = 0.0F;
for (int counter = 0; counter < strLength; ++counter) {
    thsPlace = 0.1F * thsPlace;
}

But what is a non-string, arithmetic approach to solving this?


Solution

  • pseudocode:

    n / pow(10, floor(log10(n))+1)