最好的方式得到的十进制数整数部分 [英] Best way to get whole number part of a Decimal number

查看:195
本文介绍了最好的方式得到的十进制数整数部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是返回一个小数(在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.

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

这篇关于最好的方式得到的十进制数整数部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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