doukan6564 2018-01-02 02:35
浏览 51
已采纳

检查数字1的十进制数字

I am having hundreds of numbers like:

0.00100000
0.01000000
0.01000000
1.00000000
0.00001000
0.00000100

I need to check where the number 1 is in those number, so basicly

1.00000000 = 1
0.10000000 = 2
0.01000000 = 3

I tried Round() function, but it sometimes prints numbers like 1.E-6 or something like that, I need exact location of number 1.

Thank you very much for help.

  • 写回答

3条回答 默认 最新

  • dongshan2680 2018-01-02 03:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制