我如何检查一个数字是正的或负的c# ?
当前回答
if (num < 0) {
//negative
}
if (num > 0) {
//positive
}
if (num == 0) {
//neither positive or negative,
}
或者使用else if
其他回答
你们这些年轻人,还有你们喜欢的"不到"标志。
在我的时代,我们必须使用Math.abs(num) != num //number是负的!
你只需要比较是否值和它的绝对值相等:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
bool positive = number > 0;
bool negative = number < 0;
对于32位有符号整数,例如System。Int32,也就是c#中的int:
bool isNegative = (num & (1 << 31)) != 0;
if (num < 0) {
//negative
}
if (num > 0) {
//positive
}
if (num == 0) {
//neither positive or negative,
}
或者使用else if
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和