如何从C#中的double值获得小数点后的值?

时间:2022-02-28 17:07:28

I would like to get the decimal value from a double value.

我想从double值中获取十进制值。

For example:

例如:

23.456 ->  0.456
11.23  ->  0.23

Could anyone let me know how to do this in C#??

任何人都可以让我知道如何在C#中做到这一点?

Thanks, Mahesh

谢谢,马赫什

5 个解决方案

#1


15  

x - Math.Floor(x);

x - Math.Floor(x);

text to bring up to 30 chars

文字带来30个字符

#2


48  

There is no Method in System.Math that specifically does this, but there are two which provide the way to get the integer part of your decimal, depending on how you wish negative decimal numbers to be represented.

System.Math中没有专门执行此操作的方法,但有两个提供了获取小数的整数部分的方法,具体取决于您希望如何表示负十进制数。

Math.Truncate(n) will return the number before the decimal point. So, 12.3 would return 12, and -12.3 would return -12. You would then subtract this from your original number.

Math.Truncate(n)将返回小数点前的数字。因此,12.3将返回12,-12.3将返回-12。然后,您将从原始数字中减去此值。

n - Math.Truncate(n) would give 0.3 for both 12.3 and -12.3.

n - Math.Truncate(n)对12.3和-12.3都给出0.3。

Using similar logic, Math.Floor(n) returns the whole number lower than the decimal point, and Math.Ceiling(n) returns the whole number higher than the decimal point. You can use these if you wish to use different logic for positive and negative numbers.

使用类似的逻辑,Math.Floor(n)返回低于小数点的整数,Math.Ceiling(n)返回高于小数点的整数。如果您希望对正数和负数使用不同的逻辑,则可以使用这些。

#3


11  

This is what the modulus operator (%) is for. It gives you the remainer when dividing the first operand by the second. Just divide the number you want the decimal of by 1.

这就是模数运算符(%)的用途。在将第一个操作数除以第二个操作数时,它会为您提供重新存储器。只需将小数点后的数字除以1即可。

Ex:

例如:

decimal d = new Decimal(23.456);
d = d % 1;

// d = 0.456

[EDIT]

[编辑]

After reading Nellius's comment about my post I tested it out. When using doubles the modulus operator actually returns 0.45599999999999952. My answer is in fact incorrect.

在阅读了Nellius关于我的帖子的评论后,我测试了它。当使用双精度时,模数运算符实际返回0.45599999999999952。我的回答实际上是错误的。

[/EDIT]

[/编辑]

Reference: http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

参考:http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

#4


3  

I use this method when i calculate offsets.

我在计算偏移时使用这种方法。

double numberToSplit = 4.012308d;
double decimalresult = numberToSplit - (int)numberToSplit; //4.012308 - 4 = 0.012308

#5


0  

Try this.

尝试这个。

    Dim numberToSplit As Double = 4.52121
    Dim decimalresult As Double = numberToSplit - Convert.ToInt64(numberToSplit)
    MsgBox(decimalresult)

#1


15  

x - Math.Floor(x);

x - Math.Floor(x);

text to bring up to 30 chars

文字带来30个字符

#2


48  

There is no Method in System.Math that specifically does this, but there are two which provide the way to get the integer part of your decimal, depending on how you wish negative decimal numbers to be represented.

System.Math中没有专门执行此操作的方法,但有两个提供了获取小数的整数部分的方法,具体取决于您希望如何表示负十进制数。

Math.Truncate(n) will return the number before the decimal point. So, 12.3 would return 12, and -12.3 would return -12. You would then subtract this from your original number.

Math.Truncate(n)将返回小数点前的数字。因此,12.3将返回12,-12.3将返回-12。然后,您将从原始数字中减去此值。

n - Math.Truncate(n) would give 0.3 for both 12.3 and -12.3.

n - Math.Truncate(n)对12.3和-12.3都给出0.3。

Using similar logic, Math.Floor(n) returns the whole number lower than the decimal point, and Math.Ceiling(n) returns the whole number higher than the decimal point. You can use these if you wish to use different logic for positive and negative numbers.

使用类似的逻辑,Math.Floor(n)返回低于小数点的整数,Math.Ceiling(n)返回高于小数点的整数。如果您希望对正数和负数使用不同的逻辑,则可以使用这些。

#3


11  

This is what the modulus operator (%) is for. It gives you the remainer when dividing the first operand by the second. Just divide the number you want the decimal of by 1.

这就是模数运算符(%)的用途。在将第一个操作数除以第二个操作数时,它会为您提供重新存储器。只需将小数点后的数字除以1即可。

Ex:

例如:

decimal d = new Decimal(23.456);
d = d % 1;

// d = 0.456

[EDIT]

[编辑]

After reading Nellius's comment about my post I tested it out. When using doubles the modulus operator actually returns 0.45599999999999952. My answer is in fact incorrect.

在阅读了Nellius关于我的帖子的评论后,我测试了它。当使用双精度时,模数运算符实际返回0.45599999999999952。我的回答实际上是错误的。

[/EDIT]

[/编辑]

Reference: http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

参考:http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

#4


3  

I use this method when i calculate offsets.

我在计算偏移时使用这种方法。

double numberToSplit = 4.012308d;
double decimalresult = numberToSplit - (int)numberToSplit; //4.012308 - 4 = 0.012308

#5


0  

Try this.

尝试这个。

    Dim numberToSplit As Double = 4.52121
    Dim decimalresult As Double = numberToSplit - Convert.ToInt64(numberToSplit)
    MsgBox(decimalresult)