• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

寻找的名额双小数点后

用户头像
it1352
帮助1

问题说明

我有一个Double值:

I have a Double value:

double a = 4.5565;



这是什么(在这种情况下4)小数点后计算的位数的最简单的方法

What is the easiest way to calculate the number of digits after the decimal point (4 in this case).

我知道我可以转换为字符串,并做了拆分,并采取长度。但有一个更简单的方法?

I know that I can convert to string and do a split and take the length. But is there an easier way?

正确答案

#1

有没有简单的方法,特别是数字的位数从数学上讲可能会远远超过显示。例如,4.5565实际存储为 4.556499999999999772626324556767940521240234375 (感谢哈罗德用于计算)。你不太可能找到这个问题的一个有用的解决方案。

There's no easy way, especially since the number of digits mathematically speaking might be far more than displayed. For example, 4.5565 is actually stored as 4.556499999999999772626324556767940521240234375 (thanks to harold for calculating that). You're very unlikely to find a useful solution to this problem.

修改

您的可能的想出一些算法,它的工作原理是这样的:如果你计算十进制表示,你会发现一定数量的连续787-9(或零),你圆向上(或向下)该系列787-9(或零点之前的最后的地方)开始。我怀疑你会发现更多的麻烦在这条道路比你照理。

You could come up with some algorithm that works like this: if, as you calculate the decimal representation, you find a certain number of 9s (or zeros) in succession, you round up (or down) to the last place before the series of 9s (or zeros) began. I suspect that you would find more trouble down that road than you would anticipate.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcebhjb
系列文章
更多 icon
同类精品
更多 icon
继续加载