获取数字的小数部分和小数点后的位数(C#)

时间:2022-06-02 17:08:06

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等。

4 个解决方案

#1


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。#”),这也将导致与上面的代码示例产生的较低数字相同的输出。

#2


You can get all of the relevant information from the Decimal.GetBits method assuming you really mean System.Decimal. (If you're talking about decimal formatting of a float/double, please clarify the question.)

假设您的确意味着System.Decimal,您可以从Decimal.GetBits方法获取所有相关信息。 (如果你在谈论浮点数/双精度的十进制格式,请澄清问题。)

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 significant decimal 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,否则操纵96位整数将需要一些繁琐的算术。

To be honest, you'll get a simpler solution by using the built in formatting with CultureInfo.InvariantCulture and then finding everything to the right of "."

说实话,通过使用CultureInfo.InvariantCulture的内置格式,然后查找“。”右侧的所有内容,您将获得更简单的解决方案。

#3


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

#4


You could use the Int() function to get the whole number component, then subtract from the original.

您可以使用Int()函数获取整数组件,然后从原始组件中减去。

#1


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。#”),这也将导致与上面的代码示例产生的较低数字相同的输出。

#2


You can get all of the relevant information from the Decimal.GetBits method assuming you really mean System.Decimal. (If you're talking about decimal formatting of a float/double, please clarify the question.)

假设您的确意味着System.Decimal,您可以从Decimal.GetBits方法获取所有相关信息。 (如果你在谈论浮点数/双精度的十进制格式,请澄清问题。)

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 significant decimal 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,否则操纵96位整数将需要一些繁琐的算术。

To be honest, you'll get a simpler solution by using the built in formatting with CultureInfo.InvariantCulture and then finding everything to the right of "."

说实话,通过使用CultureInfo.InvariantCulture的内置格式,然后查找“。”右侧的所有内容,您将获得更简单的解决方案。

#3


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

#4


You could use the Int() function to get the whole number component, then subtract from the original.

您可以使用Int()函数获取整数组件,然后从原始组件中减去。