C# - 转换浮点数为int ...并改变取决于剩余的INT [英] C# - Converting a float to an int... and changing the int depending on the remainder

查看:189
本文介绍了C# - 转换浮点数为int ...并改变取决于剩余的INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个真正的新手问题(当然,我敢肯定它是),但我有一个浮动就是被退回,我需要将其转换成一个快速和有效的方法 INT

This is probably a really newbie question (well, I'm pretty sure it is), but I have a float that's being returned and I need a quick and efficient way of turning it into an int.

很简单,但我有一个例外。如果在浮动的剩余比0.0其他什么话,我想增加在内部

Pretty simple, but I have an exception. If the remainder of the float is anything other than .0 then I want to increment the int.

一些简单的例子:

浮= 98.0,INT = 98结果
浮动= 98.1,INT = 99结果
浮动= 6.6, 。INT = 7结果

Float = 98.0, Int = 98
Float = 98.1, Int = 99
Float = 6.6, Int = 7
etc.

推荐答案

这应该这样做:

int myInt = (int)Math.Ceiling(myFloat);

这篇关于C# - 转换浮点数为int ...并改变取决于剩余的INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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