c# 如何实现float 取整数

u010703853 2014-08-21 07:32:01
打个比方:

当值是 1.2 时 我想要的结果就是 2 ;
当值是 0.9 时 我想要的结果就是 1。
。。。。。。。

简而言之,就是有小数位的,整数+1 而且只取整数部分 求解!!!!
...全文
3841 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
devilcom 2014-08-28
  • 打赏
  • 举报
回复
有现在的方法为啥不用
  • 打赏
  • 举报
回复
引用 17 楼 hjgzj 的回复:
[quote=引用 15 楼 smthgdin 的回复:] 直接取整+1呢?
如果是整数 就错了[/quote] 先判断是否为整数, 在判断是否需+1
  • 打赏
  • 举报
回复
引用 15 楼 smthgdin 的回复:
直接取整+1呢?
如果是整数 就错了
  • 打赏
  • 举报
回复
引用 4 楼 u014253788 的回复:
同意一楼的说法: float i = 4.6f; float j = 7.9f; Console.WriteLine(Math.Ceiling(i)); Console.WriteLine(Math.Ceiling(j)); Console.Read(); 结果为:5 8
解决了!!! 14楼的回复,很全面,谢谢! 15楼的方法,怎么没有想到呢 呵呵!!! 再次对大家表示感谢!!!
smthgdin_020 2014-08-22
  • 打赏
  • 举报
回复
直接取整+1呢?
freecodex 2014-08-22
  • 打赏
  • 举报
回复 1
对浮点数处理的常见手段: 1 向上取整
Math.Ceiling()
2 向下取整
Math.Floor()
3 四舍六入五成双
Math.Round()
4 四舍五入
var d=2.5;
var r=Math.Round(d-(int)d+1)-1+(int)d;
http://www.yayawow.com/%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0c%E7%9A%84%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5/
threenewbee 2014-08-22
  • 打赏
  • 举报
回复
引用 12 楼 Z65443344 的回复:
[quote=引用 11 楼 caozhy 的回复:] 何错之有。
你给的是四舍五入,楼主要进一法
引用 10 楼 u012948520 的回复:

int  GetInt(float value)
{
int result = value / 1;
return result + 1;
}
你这个如果本来就是整数,就出错了 [/quote] 晕,没仔细看,给那个“四舍五入”的tag搞糊涂了。
於黾 2014-08-22
  • 打赏
  • 举报
回复
引用 11 楼 caozhy 的回复:
何错之有。
你给的是四舍五入,楼主要进一法
引用 10 楼 u012948520 的回复:

int  GetInt(float value)
{
int result = value / 1;
return result + 1;
}
你这个如果本来就是整数,就出错了
threenewbee 2014-08-22
  • 打赏
  • 举报
回复
引用 8 楼 huwei001982 的回复:
[quote=引用 6 楼 caozhy 的回复:] 你想自己写的话可以这么写 float x = 1.2f; int result = (int)(x + 0.5f);
你这个是错的, 还是用 ceiling 吧[/quote] 何错之有。
白衣如花 2014-08-22
  • 打赏
  • 举报
回复

int  GetInt(float value)
{
int result = value / 1;
return result + 1;
}
白衣如花 2014-08-22
  • 打赏
  • 举报
回复
引用 8 楼 huwei001982 的回复:
[quote=引用 6 楼 caozhy 的回复:] 你想自己写的话可以这么写 float x = 1.2f; int result = (int)(x + 0.5f);
你这个是错的, 还是用 ceiling 吧[/quote] 除以1加1呢?
huwei001982 2014-08-22
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
你想自己写的话可以这么写 float x = 1.2f; int result = (int)(x + 0.5f);
你这个是错的, 还是用 ceiling 吧
白衣如花 2014-08-22
  • 打赏
  • 举报
回复
引用 12 楼 Z65443344 的回复:
[quote=引用 11 楼 caozhy 的回复:] 何错之有。
你给的是四舍五入,楼主要进一法
引用 10 楼 u012948520 的回复:

int  GetInt(float value)
{
int result = value / 1;
return result + 1;
}
你这个如果本来就是整数,就出错了 [/quote] 是的。。
小灰狼 2014-08-22
  • 打赏
  • 举报
回复
float a = 1.234f; int b = (int)(a + 0.5);
ZA0810 2014-08-21
  • 打赏
  • 举报
回复
Math.Ceiling
threenewbee 2014-08-21
  • 打赏
  • 举报
回复
你想自己写的话可以这么写 float x = 1.2f; int result = (int)(x + 0.5f);
uppaway 2014-08-21
  • 打赏
  • 举报
回复
math.ceiling
年华易逝1993 2014-08-21
  • 打赏
  • 举报
回复
同意一楼的说法:
float i = 4.6f;
float j = 7.9f;

Console.WriteLine(Math.Ceiling(i));
Console.WriteLine(Math.Ceiling(j));
Console.Read();

结果为:5
8
霜寒月冷 2014-08-21
  • 打赏
  • 举报
回复
 private int foattoint(double f)
        {
            int index = f.ToString ().IndexOf(".");
            int  Z = Convert.ToInt32 (f.ToString().Substring(0, index));                        
            int  L=Convert.ToInt32 ( f.ToString ().Substring(index + 1));
            if (Convert .ToInt16 (L)!=0)
            {
            L=1;
            }
            return Z + L;
        }
帮你写了个转换的方法
qq_19885071 2014-08-21
  • 打赏
  • 举报
回复
你这个简单 double a =12.2 int b; if (a % 1 != 0); { b= (int)a + 1; } 如果要四舍五入要这样 double aa = 199.5; int b = (int)( aa + 0.5);
加载更多回复(1)

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧