473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

atoi and atof Equivalents in .NET

32
Hello,

Are there any equivalents to the c/c++ functions atoi and atof in the .NET framework? I'd like to be able to parse a string like these two functions, but I haven't been able to find anything that works near as good. Most of the .NET functions I'm aware of are not very robust.

Thank.
Sep 17 '08 #1
6 2733
Curtis Rutland
3,256 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. string s = "12345";
  2. int i = Convert.ToInt32(s);
  3. s = "123.45";
  4. double d = Convert.ToDouble(s);
  5.  
This can convert from many other data types as well. As for float, I think you'd have to cast from a double.

There is also:
Expand|Select|Wrap|Line Numbers
  1. int i = Integer.Parse("12345");
  2. double d = Double.Parse("123.45");
  3.  
And there is also the TryParse methods.
Sep 17 '08 #2
poe
32
Expand|Select|Wrap|Line Numbers
  1. string s = "12345";
  2. int i = Convert.ToInt32(s);
  3. s = "123.45";
  4. double d = Convert.ToDouble(s);
  5.  
This can convert from many other data types as well. As for float, I think you'd have to cast from a double.

There is also:
Expand|Select|Wrap|Line Numbers
  1. int i = Integer.Parse("12345");
  2. double d = Double.Parse("123.45");
  3.  
And there is also the TryParse methods.
I've tried all the TryParse, Parse and Convert methods, but like I mentioned above, they aren't very robust. For example, lets say you have a string like "1.2 seconds" or "16.5 MB" any of the above functions will throw an exception unlike their c/c++ counterparts.
Sep 17 '08 #3
cloud255
427 Expert 256MB
I've tried all the TryParse, Parse and Convert methods, but like I mentioned above, they aren't very robust. For example, lets say you have a string like "1.2 seconds" or "16.5 MB" any of the above functions will throw an exception unlike their c/c++ counterparts.

for this you need to look at each individual character in the string or edit the UI so that the user will not enre "1.2 seconds" but rather enter 1.2 and then select the time frame from a control like a combo box.
Sep 17 '08 #4
Plater
7,872 Expert 4TB
For this I would agree with cloud. It's not the lack of robustness in the Parse() functions. (The overloads have some pretty good power behind them) but more a fault in the UI for allowing units and other text into the numeric string.
There are a number of regular expression and/or character match functions (.IsDigit for example) that could be used to step through the string and pull out only the usable sections.
Sep 17 '08 #5
balabaster
797 Expert 512MB
For this I would agree with cloud. It's not the lack of robustness in the Parse() functions. (The overloads have some pretty good power behind them) but more a fault in the UI for allowing units and other text into the numeric string.
There are a number of regular expression and/or character match functions (.IsDigit for example) that could be used to step through the string and pull out only the usable sections.
Agreed, if you're looking for the first characters that are assumed to be numeric, then you should be seeking something like

Expand|Select|Wrap|Line Numbers
  1. Dim val As Double = Regex.Match(InputString, "^\d+(\.\d+)?|(\.\d+)(?=[^\D\.])").Value;
That will strip out any leading valid number. For instance 123.45.678.9 it will only pick out 123.45, likewise 123.45Hello World it will only pick out 123.45 and "123.45 Hello World" it will still only pick out 123.45.

I have to agree with one of the previous posters - this sounds like a bad design idea. A numeric input should only allow numeric input. If someone types in something else, there should be an exception raised and it should be caught and handled properly rather than compensating for bad input.
Sep 17 '08 #6
Curtis Rutland
3,256 Expert 2GB
I'll have to remember that regex. Nice one.
Sep 17 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
4
by: Sharon | last post by:
hi all what are the reverse functions of atof( ), atoi( ) and atol( ) ? I want to change sth to string thx!
6
by: Sreekanth | last post by:
Hello, Am trying to convert a string to float. Am using atof() for that purpose. But the return value for atof is same for the string "0.0" and for some invalid input "Invalid". Can any body...
15
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
3
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string...
1
by: migurus | last post by:
I see that atof and strtod both recognize "INF", or "+INF", or "-INF" as special case and return accordingly infinite value. But atoi and strtol do not know about this special case - why? Is there...
50
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.