我如何检查一个数字是正的或负的c# ?
当前回答
public static bool IsPositive<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) > 0;
}
其他回答
bool isNegative(int n) {
int i;
for (i = 0; i <= Int32.MaxValue; i++) {
if (n == i)
return false;
}
return true;
}
你只需要比较是否值和它的绝对值相等:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
的数学。符号法是一种方法。它将返回-1表示负数,1表示正数,0表示等于0(即0没有符号)。如果双精度变量和单精度变量等于NaN,则会引发异常(ArithmeticException)。
num < 0 // number is negative
int j = num * -1;
if (j - num == j)
{
// Num is equal to zero
}
else if (j < i)
{
// Num is positive
}
else if (j > i)
{
// Num is negative
}
推荐文章
- c# XML文档网站链接
- 如何从日期时间获得完整的月份名称
- 在c#中创建一个对象的副本
- 通过字符串获取c#动态属性的值
- 我如何检查一个数字是正的或负的c# ?
- 无法找到testhost.dll。请发布测试项目并重试
- 什么是indexoutofraneexception / argumentoutofraneexception,我如何修复它?
- DbEntityValidationException -我如何容易地告诉是什么导致了错误?
- 嵌套等待平行。ForEach
- 我如何执行插入和返回插入的身份与Dapper?
- 如何从c#窗体返回一个值?
- 转换JSON字符串到c#对象
- Try /catch +使用,语法正确
- 在using块的中间返回
- 找到未使用的代码