c/c++语言开发共享获取数字的第一个小数位

我有一个例如: 2.4444444 。 我需要在点后得到第一个数字 – 在我的情况下它是4 。 怎么实现呢?

    怎么样

     ( (int)(floor( fabs( num ) * 10 ) ) ) % 10 

    把它们拿出来! 试过它,希望它有所帮助

     #include int main() { int b; float a; a=2.4444; 'load some value b=(int)a; 'typecast it into integer you get 2 into b variable a=ab; ' subtract b from a and you will get decimal point value 0.4444 a=a*10; ' multiplying with 10 gives 4.444 b=(int)a; ' now apply the same logic again printf("%d",b); 'outputs 4 } 

    使用这些链接更新写入这些function

    从C中的浮点数中提取小数部分

    它可以很简单

     num*10%10 

     (int)(num * 10) - ((int)num) * 10 

      以上就是c/c++开发分享获取数字的第一个小数位相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/550138.html

      (0)
      上一篇 2021年1月14日
      下一篇 2021年1月14日

      精彩推荐