C# 获取十进制数的整数部分的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get whole number part of a Decimal number
提问by Yaakov Ellis
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).
返回小数的整数部分的最佳方法是什么(在 C# 中)?(这必须适用于可能不适合 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.
这样做的目的是:我要插入数据库中的十进制 (30,4) 字段,并希望确保我不会尝试插入比该字段过长的数字。确定小数的整数部分的长度是此操作的一部分。
采纳答案by Yaakov Ellis
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).
顺便说一句, (int)Decimal.MaxValue 会溢出。您无法获得小数的“int”部分,因为小数太大而无法放入 int 框中。刚刚检查过......它甚至太大了很长时间(Int64)。
If you want the bit of a Decimal value to the LEFT of the dot, you need to do this:
如果您想要点的左侧的 Decimal 值的位,您需要执行以下操作:
Math.Truncate(number)
and return the value as... A DECIMAL or a DOUBLE.
并将值返回为...一个 DECIMAL 或一个 DOUBLE。
edit: Truncate is definitely the correct function!
编辑:截断绝对是正确的功能!
回答by Noldorin
You just need to cast it, as such:
您只需要投射它,如下所示:
int intPart = (int)343564564.4342
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.
如果您仍想在以后的计算中将其用作小数,那么 Math.Truncate(或者可能是 Math.Floor,如果您想要负数的某种行为)就是您想要的函数。
回答by Keith
Depends on what you're doing.
取决于你在做什么。
For instance:
例如:
//bankers' rounding - midpoint goes to nearest even
GetIntPart(2.5) >> 2
GetIntPart(5.5) >> 6
GetIntPart(-6.5) >> -6
or
或者
//arithmetic rounding - midpoint goes away from zero
GetIntPart(2.5) >> 3
GetIntPart(5.5) >> 6
GetIntPart(-6.5) >> -7
The default is always the former, which can be a surprise but makes very good sense.
默认值始终是前者,这可能令人惊讶,但非常有意义。
Your explicit cast will do:
你的明确演员会做:
int intPart = (int)343564564.5
// intPart will be 343564564
int intPart = (int)343564565.5
// intPart will be 343564566
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:
我会做:
Math.Floor(Math.Abs(number));
Also check the size of your decimal
- they can be quite big, so you may need to use a long
.
还要检查您的大小decimal
- 它们可能很大,因此您可能需要使用long
.
回答by Mark Carpenter
I think System.Math.Truncateis what you're looking for.
我认为System.Math.Truncate就是你要找的。
回答by Amit Gohel
Very easy way to separate value and its fractional part value.
分离值及其小数部分值的非常简单的方法。
double d = 3.5;
int i = (int)d;
string s = d.ToString();
s = s.Replace(i + ".", "");
s is fractional part = 5 and
i is value as integer = 3
s 是小数部分 = 5 并且
i 是整数值 = 3
回答by Mattheu Norwood
Public Function getWholeNumber(number As Decimal) As Integer
Dim round = Math.Round(number, 0)
If round > number Then
Return round - 1
Else
Return round
End If
End Function
回答by luka
I hope help you.
我希望能帮到你。
/// <summary>
/// Get the integer part of any decimal number passed trough a string
/// </summary>
/// <param name="decimalNumber">String passed</param>
/// <returns>teh integer part , 0 in case of error</returns>
private int GetIntPart(String decimalNumber)
{
if(!Decimal.TryParse(decimalNumber, NumberStyles.Any , new CultureInfo("en-US"), out decimal dn))
{
MessageBox.Show("String " + decimalNumber + " is not in corret format", "GetIntPart", MessageBoxButtons.OK, MessageBoxIcon.Error);
return default(int);
}
return Convert.ToInt32(Decimal.Truncate(dn));
}