最好的方式得到的十进制数整数部分最好的、整数、方式、部分

2023-09-03 09:26:32 作者:何以琛

什么是返回一个小数(在C#)的整数部分的最好方法是什么? (此为讨取非常大的数字,可能不适合一个int)。

What is the best way to return the whole number part of a decimal (in c#)? (This has to work for very large numbers that may not fit into an int).

GetIntPart(343564564.4342) >> 343564564
GetIntPart(-323489.32) >> -323489
GetIntPart(324) >> 324

这样做的目的是:我插入一个十进制(30,4)领域中的分贝,并希望确保我不要尝试插入数字比过长的领域。确定所述十进制的整数部分的长度是该操作的一部分。

The purpose of this is: I am inserting into a decimal (30,4) field in the db, and want to ensure that I do not try to insert a number than is too long for the field. Determining the length of the whole number part of the decimal is part of this operation.

推荐答案

到了球员的方式,(INT)Decimal.MaxValue就会溢出。你不能得到一个十进制的诠释的一部分,因为小数点太大friggen大放于INT框。刚才检查...它甚至是太大了很长(Int64的)。

By the way guys, (int)Decimal.MaxValue will overflow. You can't get the "int" part of a decimal because the decimal is too friggen big to put in the int box. Just checked... its even too big for a long (Int64).

如果你想的十进制值点左边的位,你需要做的:

If you want the bit of a Decimal value to the LEFT of the dot, you need to do this:

Math.Truncate(number)

和返回值...小数或双。

and return the value as... A DECIMAL or a DOUBLE.

编辑:截断绝对是正确的函数的