[Solved] Check where number 1 is in decimal number


I wouldn’t rely too much on the approach you posted in your answer. Use the following function instead:

function index_of_one($dec)
{
  // maximum precision is 15
  $str = str_replace('.','',sprintf('%.15f', $dec));

  $pos = strpos($str, '1');

  if ($pos === false) {
    return -1;
  }

  return ($pos + 1);
}

Example:

$dec1 = 1.00000000;
$dec2 = 0.10000000;
$dec3 = 0.00010000;

echo index_of_one($dec1); // 1
echo index_of_one($dec2); // 2
echo index_of_one($dec3); // 5

Visit this link to test it.

1

solved Check where number 1 is in decimal number