寻找的名额一个双小数点后 [英] Finding the number of places after the decimal point of a Double

查看:61
本文介绍了寻找的名额一个双小数点后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

推荐答案

有没有简单的方法,特别是数字的位数从数学上讲可能会远远超过显示。例如,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.

这篇关于寻找的名额一个双小数点后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆