小数的整数。 [英] Integer from a decimal.

查看:93
本文介绍了小数的整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在寻找一个函数,它将返回大于或等于十进制数的最小整数




例如。


3.1(十进制)返回4(整数)

3.9(十进制)返回4 (整数)

4.0(十进制)返回4(整数)


我想我可以用Math.Ceiling做某事,但这似乎需要一个Double

作为输入并返回Double。有没有一个整洁的方式,或者我将

将我的十进制数转换为Double,然后以某种方式将结果转换为

整数手?


谢谢,


克里斯。

解决方案

ChrisM写道:


大家好,


我正在寻找能够返回最低整数的函数

大于或等于十进制数。


例如。


3.1(十进制)返回4(整数)

3.9(十进制)返回4(整数)

4.0(十进制)返回4(整数)


我想我可以做某事使用Math.Ceiling,但似乎需要一个

Double作为输入并返回Double。有没有一个简洁的方法,或者我是否需要将我的十进制数转换为双数,然后以某种方式将

结果转换为整数?



您可以将十进制转换为双倍而不会出现太多问题。或者你可以

写你自己的天花板来处理这个。


static int ceil(十进制d)

{

if(d 0)

return(int)d == d? (int)d:((int)d)+ 1;

else

return(int)d;

}

-

Tom Porterfield


ChrisM< ch *********** ***@suedeyahoo.comwrote:


我正在寻找一个函数,它将返回最大的整数,即
大于或等于十进制数。


例如。


3.1(十进制)返回4(整数)

3.9(十进制)返回4(整数)

4.0(十进制)返回4(整数)


我想我可以用Math.Ceiling做某事,但是这似乎需要一个Double

作为输入并返回一个Double。有没有一个整洁的方式,或者我将

将我的十进制数转换为Double,然后以某种方式将结果转换为

整数手动?



Math.Ceiling有一个重载,它接受并返回一个小数,

然后你会转换为int :


使用System;

使用System.Diagnostics;


class测试

{

static void Main()

{

Debug.Assert(CeilingOfDecimalAsInt(3.1m)== 4);

Debug.Assert(CeilingOfDecimalAsInt(3.9m)== 4);

Debug.Assert(CeilingOfDecimalAsInt(4m)== 4);

}


static int CeilingOfDecimalAsInt(十进制m)

{

return(int)Math.Ceiling(m);

}

}


(方法名称不是真正建议的一个:)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


Tom Porterfield写道:


>我正在寻找一个函数,它将返回大于或等于十进制数的最小整数。

例如。

3.1(十进制)返回4(整数)
3.9(十进制)返回4(整数)
4.0(十进制)返回4(整数)

我我猜我可以用Math.Ceiling做某事,但这似乎需要一个
Double作为输入并返回一个Double。有一个简洁的方法,或者我是否必须将我的十进制数转换为Double,然后以某种方式将
结果转换为整数?



您可以将十进制转换为双倍而不会出现太多问题。或者你可以

写你自己的天花板来处理这个。


static int ceil(十进制d)

{

if(d 0)

return(int)d == d? (int)d:((int)d)+ 1;

else

return(int)d;

}



或者以2.0方式:


static int ceil(十进制d)

{

return(int) - (decimal.Floor(-d));

}

-

Tom Porterfield


Hi Guys,

I''m looking for a function that will return the lowest integer that is
greater than or equal to a decimal number.

eg.

3.1(decimal) returns 4(integer)
3.9(decimal) returns 4(integer)
4.0(decimal) returns 4(integer)

I guess I can do somthing with Math.Ceiling, but that seems to need a Double
as an input and returns a Double. Is there a neat way, or am I going to have
to cast my Decimal to a Double, then somehow convert the result to an
Integer by hand?

Thanks,

Chris.

解决方案

ChrisM wrote:

Hi Guys,

I''m looking for a function that will return the lowest integer that is
greater than or equal to a decimal number.

eg.

3.1(decimal) returns 4(integer)
3.9(decimal) returns 4(integer)
4.0(decimal) returns 4(integer)

I guess I can do somthing with Math.Ceiling, but that seems to need a
Double as an input and returns a Double. Is there a neat way, or am I
going to have to cast my Decimal to a Double, then somehow convert the
result to an Integer by hand?

You can convert decimal to double without too much problem. Or you can
write your own Ceiling to handle this.

static int ceil(decimal d)
{
if (d 0)
return (int)d == d ? (int)d : ((int)d) + 1;
else
return (int)d;
}
--
Tom Porterfield


ChrisM <ch**************@suedeyahoo.comwrote:

I''m looking for a function that will return the lowest integer that is
greater than or equal to a decimal number.

eg.

3.1(decimal) returns 4(integer)
3.9(decimal) returns 4(integer)
4.0(decimal) returns 4(integer)

I guess I can do somthing with Math.Ceiling, but that seems to need a Double
as an input and returns a Double. Is there a neat way, or am I going to have
to cast my Decimal to a Double, then somehow convert the result to an
Integer by hand?

There''s an overload of Math.Ceiling which takes and returns a decimal,
which you''d then cast to int:

using System;
using System.Diagnostics;

class Test
{
static void Main()
{
Debug.Assert(CeilingOfDecimalAsInt(3.1m)==4);
Debug.Assert(CeilingOfDecimalAsInt(3.9m)==4);
Debug.Assert(CeilingOfDecimalAsInt(4m)==4);
}

static int CeilingOfDecimalAsInt(decimal m)
{
return (int) Math.Ceiling(m);
}
}

(The method name isn''t a real suggested one :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Tom Porterfield wrote:

>I''m looking for a function that will return the lowest integer that is
greater than or equal to a decimal number.

eg.

3.1(decimal) returns 4(integer)
3.9(decimal) returns 4(integer)
4.0(decimal) returns 4(integer)

I guess I can do somthing with Math.Ceiling, but that seems to need a
Double as an input and returns a Double. Is there a neat way, or am I
going to have to cast my Decimal to a Double, then somehow convert the
result to an Integer by hand?


You can convert decimal to double without too much problem. Or you can
write your own Ceiling to handle this.

static int ceil(decimal d)
{
if (d 0)
return (int)d == d ? (int)d : ((int)d) + 1;
else
return (int)d;
}

Or in the 2.0 manner:

static int ceil(decimal d)
{
return (int)-(decimal.Floor(-d));
}
--
Tom Porterfield


这篇关于小数的整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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