获取十进制数的整数部分的最佳方法
返回小数的整数部分(在 C# 中)的最佳方法是什么? (这必须适用于可能不适合 int 的非常大的数字)。
GetIntPart(343564564.4342) >> 343564564
GetIntPart(-323489.32) >> -323489
GetIntPart(324) >> 324
这样做的目的是:我要插入数据库中的十进制 (30,4) 字段,并希望确保我不会尝试插入对于该字段来说太长的数字。 确定小数的整数部分的长度是此操作的一部分。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您只需要对其进行转换,如下所示:
如果您仍然想在以后的计算中将其用作小数,那么 Math.Truncate (如果您想要负数的某种行为,则可能是 Math.Floor)就是您想要的函数。
You just need to cast it, as such:
If you still want to use it as a decimal in later calculations, then Math.Truncate (or possibly Math.Floor if you want a certain behaviour for negative numbers) is the function you want.
分离值及其小数部分值的非常简单的方法。
s 是小数部分 = 5 且
i 的值为整数 = 3
Very easy way to separate value and its fractional part value.
s is fractional part = 5 and
i is value as integer = 3
忘记术语的含义:“整数”在答案和问题中似乎很常见。
从数字:4中得到整数很简单:
对数字进行四舍五入,得到整数是得到整数的秘籍方法! 将其四舍五入,删除数字的非整数部分!
了解整数是什么很重要!
请提问,并以更准确的方式回答问题...
返回:
或:
是一个数学函数,从而改变数字,而不是使用整数。 您的四舍五入非整数!
Forgetting the meaning of the term: "Whole Number" seems common in the answers, and in the Question.
Getting the whole number from the number: 4 is simple:
Rounding a Number, to get a Whole Number is a cheats method of getting the Whole Numbers! Rounding it removing the Non-Whole Number part of the Number!
Its important to understand what a Whole Number is!
Please ask, and answer the questions with a bit more Accuracy Peeps...
Returns:
or:
Is a Mathematical Function, thus changing the Number, and not working with Whole Numbers. Your Rounding Non-Whole Numbers!
我希望对你有帮助。
I hope help you.
顺便说一句,(int)Decimal.MaxValue 会溢出。 您无法获得小数的“int”部分,因为小数太大而无法放入 int 框中。 刚刚检查过...它对于很长的时间来说甚至太大了(Int64)。
如果您希望 Decimal 值的位位于点的左侧,则需要执行以下操作:
并将该值返回为...DECIMAL 或 DOUBLE。
编辑:截断绝对是正确的函数!
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:
and return the value as... A DECIMAL or a DOUBLE.
edit: Truncate is definitely the correct function!
我认为 System.Math.Truncate 就是您正在寻找的。
I think System.Math.Truncate is what you're looking for.
取决于你在做什么。
例如:
或
默认值始终是前者,这可能会令人惊讶,但 很有道理。
你的明确的演员阵容就可以了:
从你提出问题的方式来看,这听起来好像这不是你想要的——你每次都想把它打倒。
我会这样做:
同时检查您的
小数
的大小 - 它们可能非常大,因此您可能需要使用long
。Depends on what you're doing.
For instance:
or
The default is always the former, which can be a surprise but makes very good sense.
Your explicit cast will do:
From the way you've worded the question it sounds like this isn't what you want - you want to floor it every time.
I would do:
Also check the size of your
decimal
- they can be quite big, so you may need to use along
.