获取一个数字的小数部分和小数点后的位数(C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1157960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the decimal part of a number and the number of places after the decimal point (C#)
提问by MT.
Does anyone know of an elegant way to get the decimal part of a number only? In particular I am looking to get the exact number of places after the decimal point so that the number can be formatted appropriately. I was wondering if there is away to do this without any kind of string extraction using the culture specific decimal separator....
有谁知道一种只获取数字小数部分的优雅方法?特别是我希望获得小数点后的确切位数,以便可以适当地格式化数字。我想知道是否可以使用特定于文化的十进制分隔符在没有任何字符串提取的情况下执行此操作....
For example
98.0 would be formatted as 98
98.20 would be formatted as 98.2
98.2765 would be formatted as 98.2765 etc.
例如
98.0 将被格式化为 98
98.20 将被格式化为 98.2
98.2765 将被格式化为 98.2765 等等。
采纳答案by Fredrik M?rk
It it's only for formatting purposes, just calling ToString
will do the trick, I guess?
它仅用于格式化目的ToString
,我猜只是调用就可以了?
double d = (double)5 / 4;
Console.WriteLine(d.ToString()); // prints 1.75
d = (double)7 / 2;
Console.WriteLine(d.ToString()); // prints 3.5
d = 7;
Console.WriteLine(d.ToString()); // prints 7
That will, of course, format the number according to the current culture (meaning that the decimal sign, thousand separators and such will vary).
当然,这将根据当前的文化来格式化数字(意味着小数点、千位分隔符等会有所不同)。
Update
更新
As Clement H points out in the comments; if we are dealing with great numbers, at some point d.ToString() will return a string with scientific formatting instead (such as "1E+16"
instead of "10000000000000000"
). One way to overcome this probem, and force the full number to be printed, is to use d.ToString("0.#")
, which will also result in the same output for lower numbers as the code sample above produces.
正如 Clement H 在评论中指出的那样;如果我们正在处理大量数字,在某些时候 d.ToString() 将返回一个带有科学格式的字符串(例如"1E+16"
代替"10000000000000000"
)。克服此问题并强制打印完整数字的一种方法是使用d.ToString("0.#")
,这也将导致与上述代码示例产生的较低数字相同的输出。
回答by Jon Skeet
You can get all of the relevant information from the Decimal.GetBitsmethod assuming you really mean System.Decimal
. (If you're talking about decimal formatting of a float/double, please clarify the question.)
您可以从Decimal.GetBits方法中获取所有相关信息,前提是您的意思是System.Decimal
。(如果您谈论的是浮点数/双精度数的十进制格式,请澄清问题。)
Basically GetBits
will return you 4 integers in an array.
基本上GetBits
会在一个数组中返回 4 个整数。
You can use the scaling factor (the fourth integer, after masking out the sign) to indicate the number of decimal places, but you should be aware that it's not necessarily the number of significantdecimal places. In particular, the decimal representations of 1 and 1.0 are different (the former is 1/1, the latter is 10/10).
您可以使用比例因子(屏蔽符号后的第四个整数)来表示小数位数,但您应该注意它不一定是有效小数位数。特别是1和1.0的十进制表示不同(前者是1/1,后者是10/10)。
Unfortunately, manipulating the 96 bit integer is going to require some fiddly arithmetic unless you can use .NET 4.0 and BigInteger
.
不幸的是,除非您可以使用 .NET 4.0 和BigInteger
.
To be honest, you'll get a simplersolution by using the built in formatting with CultureInfo.InvariantCulture
and then finding everything to the right of "."
老实说,通过使用内置格式,然后找到“。”右侧的所有内容,您将获得更简单的解决方案CultureInfo.InvariantCulture
。
回答by Fredrik M?rk
You could use the Int() function to get the whole number component, then subtract from the original.
您可以使用 Int() 函数获取整数分量,然后从原始值中减去。
回答by Fredrik M?rk
Just to expand on the point about getbits, this expression gets the scaling factor from a decimal called foo:
只是为了扩展关于 getbits 的点,这个表达式从一个叫做 foo 的小数中获取缩放因子:
(decimal.GetBits(foo)[3] & 16711680)>>16