Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am getting a string value in the given format
1)abc<br />
2)abc23232<br />
3)1,23232<br />
4)1221<br />
5)-32323


now how can i check
1)whether the string has integer value and
2)if it has interger value how i can check whether it is negative or not
Posted
Updated 2-Jan-12 0:23am
v2
Comments
lakshmichawala 2-Jan-12 7:37am    
Tanks for solution .if is negative how can i convert the value in the bleow format -4567.890 to (4567.890)

use this..
Int32.Parse[^]
 
Share this answer
 
v2
(1) There's the Int32.TryParse[^] method for that (please read the documentation).
(2) Once TryParse returns true you have the integer and you may test if it is less than zero.
 
Share this answer
 
Try this,

C#
int checkNumber;
if (int.TryParse(TextBox2.Text, out checkNumber) == false)
{
    //Entered number is not interger;
}
else
{
    if (Convert.ToInt16(TextBox2.Text) < 0)
    {
        //Entered no. is negative
    }
    else
    {
        //Entered no. is positive
    }
}
 
Share this answer
 
Comments
lakshmichawala 2-Jan-12 7:36am    
Thanks for ur solution .if is negative how can i convert the value in the bleow format -4567.890 to (4567.890)
Supriya Srivastav 3-Jan-12 1:09am    
absolute value can be calculate by,
decimal val = Math.Abs(Convert.ToDecimal(TextBox1.Text));
Supriya Srivastav 3-Jan-12 1:47am    
If it solved your problem,then mark it as solved.
Thanks.
Try
C#
public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
    Double result;
    return Double.TryParse(val,NumberStyle,
        System.Globalization.CultureInfo.CurrentCulture,out result);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900