PHP - Find the number of zeros in a decimal number

后端 未结 3 1315
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 22:48

Let\'s say we have 0.00045. I want to find a way to count the number of \"significant\" zeros after the decimal point (3 in this case). I\'ve been trying to implement st

3条回答
  •  梦谈多话
    2021-01-23 23:35

    Match the first consecutive zeroes in the mantissa:

    $number = 123.0000000240003;
    
    preg_match("/^(0+)/", explode('.', $number)[1], $matches);
    
    echo strlen($matches[0]); // echoes 7
    

提交回复
热议问题