C#中怎样判断是正整数

paradise885 2007-09-03 05:22:07
C#中怎样判断是正整数?
谢谢
...全文
4816 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
侃大川 2007-09-04
  • 打赏
  • 举报
回复
tsy_zx() 的见解给予启发,简练,谢谢。
VB中有判断字符串是否有效数字的函数,但C++.Net、C#.Net 以及VB.Net将“Is数字”函数舍去了(?起码我未找到),启用了判断字符是否是数字的方法,实在是败笔。
在C++.Net、C#.Net 以及VB.Net中,判断文本框输入的数字是否有效,只有自己编制判定函数了。
有效数字的字符包括:0~9、可能存在的唯一的小数点、e指数形式、数字串最左端的负号(可能)、E后缀的负号(可能)等字符。需要一一判别。有时为输入方便,0.89 输入成 .89 ,即小数点出现在左端,也应该允许。

见解是否正确?

jx火狐 2007-09-04
  • 打赏
  • 举报
回复
收藏一下..
tsy_zx 2007-09-04
  • 打赏
  • 举报
回复
public bool IsNumR(string str)
{
double r;
if (!double.TryParse(str,out r)) return false;
if (r <= 0) return false;
if ((int)r != r) return false;
return true;
}
看看行不行?
baicai1014 2007-09-04
  • 打赏
  • 举报
回复
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
private bool IsNumeric(string str)
{
if (str == null || str.Length == 0)
return false;
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] bytestr = ascii.GetBytes(str);
foreach(byte c in bytestr)
{
if (c < 48 || c > 57)
{
return false;
}
}
return true;
}
sz984141 2007-09-04
  • 打赏
  • 举报
回复
判断有没小数,并和0比较
fengzheng0306 2007-09-04
  • 打赏
  • 举报
回复
/// <summary>
/// 判断一个字符串是否是正数型的字符串
/// </summary>
/// <param name="strValue">字符串</param>
/// <returns>是则返回true,否则返回false</returns>
public static bool IsUnsign(string strValue)
{
return Regex.IsMatch(strValue, @"^\d*[.]?\d*$");
}
tongxuechen1982 2007-09-04
  • 打赏
  • 举报
回复
void GetInt(Objcect o)
{
int k=0;
if (o.GetType() == typeof(int))
{
k = (int)o;
if (k > 0)
Console.WriteLine(k);
}
}

LZ问题没阐述清楚,按照我的理解应该是给定一个OBJECT类型,然后再判断这个Object是不是正整数
watson110 2007-09-04
  • 打赏
  • 举报
回复
up~~~~~~
JustLovePro 2007-09-04
  • 打赏
  • 举报
回复
mark!
monkey2307 2007-09-04
  • 打赏
  • 举报
回复
菜鸟法:
string[] a=i.tostring().split('.');
if(a.length>1||i<0) return false;
不知道这样行不行,纯想想,没尝试过.
yitian130 2007-09-04
  • 打赏
  • 举报
回复
up
LQP039 2007-09-04
  • 打赏
  • 举报
回复
我看还不如直接看一下有没小数点与‘-’有就不是了。。这样来得比较容易

必须首先检查是否是合法的数字形式,并且 2.0 算不算 正数也得看用户需要了.
best8625 2007-09-04
  • 打赏
  • 举报
回复
我看还不如直接看一下有没小数点与‘-’有就不是了。。这样来得比较容易
ghost6000 2007-09-04
  • 打赏
  • 举报
回复
用正则表达式
private bool MatchNumber(string str)
{
Regex reg = new Regex("^\\d+");
Match number = reg.Match(str);
return number.Success;
}
private void main()
{
string str = "456";
if(MatchNumber(str))
{
//str为正整数
}
else
{
//str为其它字符或是数字
}
}
ghost6000 2007-09-04
  • 打赏
  • 举报
回复
用正则表达式
a523194491 2007-09-04
  • 打赏
  • 举报
回复
添加对Microsoft Visaul Basic .Net Runtime 程序集的引用就可以用VB.net中的函数了

public bool IsIntNumber(string str)
{
if (Microsoft.VisualBasic.Information.IsNumeric(str))
{
double t = System.Convert.ToDouble(str);
return (System.Convert.ToInt32(t) == t && System.Convert.ToInt32(t) > 0);
}
else
return false;
}
howbigsea 2007-09-04
  • 打赏
  • 举报
回复

TryParse 方法可以解决了
bluestream 2007-09-03
  • 打赏
  • 举报
回复
//只能输入数字
if(!char.isdigit(e.keychar))
{
e.handled = true;
}
ycg_893 2007-09-03
  • 打赏
  • 举报
回复
Math.Abs 取绝对值与原值比较
lovefootball 2007-09-03
  • 打赏
  • 举报
回复
Sorry
我的错了~~~~~

还是用正则吧~~~
加载更多回复(2)

110,499

社区成员

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

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

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