-
Converting input strings to integers
Is there a standard .NET way to convert input strings to their closest integer
equivalent? In VB.NET I basically do a "CInt" and it takes care of most of
the problem (except for null values).
My first (pathetic non-working) attempt is the following - it currently fails
on non-numeric input or non-integer numbers. Am I totally out to lunch? Or
will I need to convert to a char array and look at each character, etc? I'm
willing to methodically go through that process, so I don't expect anyone
to write that code for me, but I have a hunch I'm going down the wrong track
on this. And of course I've named it "CInt" as any VB diehard would have
done 
public static int CInt(object oValue)
{
if (oValue == null || System.Convert.IsDBNull(oValue))
{
//default null to 0:
return 0;
}
string sString = oValue.ToString().Trim();
if (sString == "")
{
//default empty string to 0:
return 0;
}
return int.Parse(sString, NumberStyles.Any);
}
-
Re: Converting input strings to integers
Look at the various integer types' Parse method. Or see the Convert.ToInt32
method (also see Convert to convert to other integer types). Or feel free to
import the VB namespace and use CInt (and other VB commands) directly.
"Dave" <dave_doknjas@yahoo.ca> wrote in message
news:3f319a37$1@tnews.web.devx.com...
>
> Is there a standard .NET way to convert input strings to their closest
integer
> equivalent? In VB.NET I basically do a "CInt" and it takes care of most of
> the problem (except for null values).
>
> My first (pathetic non-working) attempt is the following - it currently
fails
> on non-numeric input or non-integer numbers. Am I totally out to lunch? Or
> will I need to convert to a char array and look at each character, etc?
I'm
> willing to methodically go through that process, so I don't expect anyone
> to write that code for me, but I have a hunch I'm going down the wrong
track
> on this. And of course I've named it "CInt" as any VB diehard would have
> done 
>
> public static int CInt(object oValue)
> {
> if (oValue == null || System.Convert.IsDBNull(oValue))
> {
> //default null to 0:
> return 0;
> }
>
> string sString = oValue.ToString().Trim();
> if (sString == "")
> {
> //default empty string to 0:
> return 0;
> }
>
> return int.Parse(sString, NumberStyles.Any);
> }
>
-
Re: Converting input strings to integers
"Russell Jones" <arj1@northstate.net> wrote:
>Look at the various integer types' Parse method. Or see the Convert.ToInt32
>method (also see Convert to convert to other integer types). Or feel free
to
>import the VB namespace and use CInt (and other VB commands) directly.
>
>"Dave" <dave_doknjas@yahoo.ca> wrote in message
>news:3f319a37$1@tnews.web.devx.com...
>>
>> Is there a standard .NET way to convert input strings to their closest
>integer
>> equivalent? In VB.NET I basically do a "CInt" and it takes care of most
of
>> the problem (except for null values).
>>
>> My first (pathetic non-working) attempt is the following - it currently
>fails
>> on non-numeric input or non-integer numbers. Am I totally out to lunch?
Or
>> will I need to convert to a char array and look at each character, etc?
>I'm
>> willing to methodically go through that process, so I don't expect anyone
>> to write that code for me, but I have a hunch I'm going down the wrong
>track
>> on this. And of course I've named it "CInt" as any VB diehard would have
>> done 
>>
>> public static int CInt(object oValue)
>> {
>> if (oValue == null || System.Convert.IsDBNull(oValue))
>> {
>> //default null to 0:
>> return 0;
>> }
>>
>> string sString = oValue.ToString().Trim();
>> if (sString == "")
>> {
>> //default empty string to 0:
>> return 0;
>> }
>>
>> return int.Parse(sString, NumberStyles.Any);
>> }
>>
>
I'm currently using the int type's Parse method from within my routine, but
the problem is (and also with the Convert namespace) is that I'll have to
resort to catching exceptions in this process and I'd rather not do that
as part of normal logic.
Import the VB namespace? *** GASP ***
Ok, maybe, if I have to.
-
Re: Converting input strings to integers
Hello Dave,
> Is there a standard .NET way to convert input strings to their closest
integer
> equivalent? In VB.NET I basically do a "CInt" and it takes care of
most of
> the problem (except for null values).
Try the static Double.TryParse method.
using System.Globalization;
bool success;
int intValue;
double doubleValue;
string x = "1234";
success = Double.TryParse( x, NumberStyles.Integer,
CultureInfo.CurrentCulture,
out doubleValue )
if( success ) intValue = (int)doubleValue;
If it can't perform the conversion it will return false, which is
better than throwing an exception.
Hope this helps,
Len
-
Re: Converting input strings to integers
"Len Weaver" <len.weaver@meritsoft.net> wrote:
>Hello Dave,
>
>> Is there a standard .NET way to convert input strings to their closest
>integer
>> equivalent? In VB.NET I basically do a "CInt" and it takes care of
>most of
>> the problem (except for null values).
>
> Try the static Double.TryParse method.
>
>using System.Globalization;
>
>bool success;
>int intValue;
>double doubleValue;
>string x = "1234";
>
>success = Double.TryParse( x, NumberStyles.Integer,
> CultureInfo.CurrentCulture,
> out doubleValue )
>
>if( success ) intValue = (int)doubleValue;
>
> If it can't perform the conversion it will return false, which is
>better than throwing an exception.
>
>Hope this helps,
>Len
>
>
Perfect!! Thanks Len!
I couldn't find anything relevant for the int type, so I didn't even bother
to look at the double type. This is exactly the kind of thing I was looking
for.
-
thanx Len, i'm kinda n00b in VC# porting myself from VB6. This is exactly what i was looking 4.
-
You could also use the IsNumeric method, but that would also require importing VB.
-
Unfortunately the TryParse is only available for Double. I wish it was available for other conversions...
-
-
Is this way working for you or are you getting errors?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
Hack, its working properly.
-
Try the following as a c# IsNumeric Function:
private bool IsNumeric(object oTst)
{
double nDbl = new double();
string strTst = Convert.ToString(oTst);
bool bNumeric = double.TryParse(strTst,
System.Globalization.NumberStyles.Any, null, out nDbl);
return bNumeric;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks