获取十进制数的整数部分的最佳方法

发布于 2024-07-12 22:04:05 字数 294 浏览 10 评论 0原文

返回小数的整数部分(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

终止放荡 2024-07-19 22:04:05

您只需要对其进行转换,如下所示:

int intPart = (int)343564564.4342

如果您仍然想在以后的计算中将其用作小数,那么 Math.Truncate (如果您想要负数的某种行为,则可能是 Math.Floor)就是您想要的函数。

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.

想念有你 2024-07-19 22:04:05

分离值及其小数部分值的非常简单的方法。

double  d = 3.5;
int i = (int)d;
string s = d.ToString();
s = s.Replace(i + ".", "");

s 是小数部分 = 5 且
i 的值为整数 = 3

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

此刻的回忆 2024-07-19 22:04:05
   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
   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
伤痕我心 2024-07-19 22:04:05

忘记术语的含义:“整数”在答案和问题中似乎很常见。

从数字:4中得到整数很简单:

1 x 4 = 4 <- A Whole Number! The first Whole Number!
2 x 4 = 8 <- A Whole Number!
3 x 4 = 12 <- A Whole Number!

对数字进行四舍五入,得到整数是得到整数的秘籍方法! 将其四舍五入,删除数字的非整数部分!

1.3 x 4 = 5.2 <- NOT a Whole Number!
1 x 343564564.4342 <- NOT a Whole Number!

了解整数是什么很重要!

4 / 1 = 4 <- A Whole Number!
4 / 2 = 2 <- A Whole Number!
4 / 3 = 1.333 recurring  <- NOT A Whole Number!

请提问,并以更准确的方式回答问题...

double A = Math.Abs(343564564.4342);
double B = Math.Floor(343564564.4342);
double C = Math.Ceiling(343564564.4342);
double D = Math.Truncate(343564564.4342);

返回:

A = 343564564.4342
B = 343564564
C = 343564565
D = 343564564

或:

double E = Math.Round(343564564.4342, 0);
E = 343564564

是一个数学函数,从而改变数字,而不是使用整数。 您的四舍五入非整数!

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:

1 x 4 = 4 <- A Whole Number! The first Whole Number!
2 x 4 = 8 <- A Whole Number!
3 x 4 = 12 <- A Whole Number!

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!

1.3 x 4 = 5.2 <- NOT a Whole Number!
1 x 343564564.4342 <- NOT a Whole Number!

Its important to understand what a Whole Number is!

4 / 1 = 4 <- A Whole Number!
4 / 2 = 2 <- A Whole Number!
4 / 3 = 1.333 recurring  <- NOT A Whole Number!

Please ask, and answer the questions with a bit more Accuracy Peeps...

double A = Math.Abs(343564564.4342);
double B = Math.Floor(343564564.4342);
double C = Math.Ceiling(343564564.4342);
double D = Math.Truncate(343564564.4342);

Returns:

A = 343564564.4342
B = 343564564
C = 343564565
D = 343564564

or:

double E = Math.Round(343564564.4342, 0);
E = 343564564

Is a Mathematical Function, thus changing the Number, and not working with Whole Numbers. Your Rounding Non-Whole Numbers!

丑疤怪 2024-07-19 22:04:05

我希望对你有帮助。

/// <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));
}

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));
}
银河中√捞星星 2024-07-19 22:04:05

顺便说一句,(int)Decimal.MaxValue 会溢出。 您无法获得小数的“int”部分,因为小数太大而无法放入 int 框中。 刚刚检查过...它对于很长的时间来说甚至太大了(Int64)。

如果您希望 Decimal 值的位位于点的左侧,则需要执行以下操作:

Math.Truncate(number)

并将该值返回为...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:

Math.Truncate(number)

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

edit: Truncate is definitely the correct function!

伴随着你 2024-07-19 22:04:05

我认为 System.Math.Truncate 就是您正在寻找的。

I think System.Math.Truncate is what you're looking for.

ぶ宁プ宁ぶ 2024-07-19 22:04:05

取决于你在做什么。

例如:

//bankers' rounding - midpoint goes to nearest even
GetIntPart(2.5) >> 2
GetIntPart(5.5) >> 6
GetIntPart(-6.5) >> -6

//arithmetic rounding - midpoint goes away from zero
GetIntPart(2.5) >> 3
GetIntPart(5.5) >> 6
GetIntPart(-6.5) >> -7

默认值始终是前者,这可能会令人惊讶,但 很有道理

你的明确的演员阵容就可以了:

int intPart = (int)343564564.5
// intPart will be 343564564

int intPart = (int)343564565.5
// intPart will be 343564566

从你提出问题的方式来看,这听起来好像这不是你想要的——你每次都想把它打倒。

我会这样做:

Math.Floor(Math.Abs(number));

同时检查您的小数的大小 - 它们可能非常大,因此您可能需要使用long

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文