converting strings to decimal

P

Paul K

I'm writing a small component that needs to be as fast as
possible. The component needs to convert a string to
decimal during the course of it's processing. However, I
need to test the string first to make sure it is
numeric. Using the is keyword doesn't work (strings
cannot be cast as decimal so false is always returned)
and catching an exception from Convert.ToDecimal or
decimal.Parse is too slow.

Does anyone know of any methods of testing strings to see
if they are numerical without converting the string?

Paul K
 
J

Jon Skeet [C# MVP]

Paul K said:
I'm writing a small component that needs to be as fast as
possible. The component needs to convert a string to
decimal during the course of it's processing. However, I
need to test the string first to make sure it is
numeric. Using the is keyword doesn't work (strings
cannot be cast as decimal so false is always returned)
and catching an exception from Convert.ToDecimal or
decimal.Parse is too slow.

Does anyone know of any methods of testing strings to see
if they are numerical without converting the string?

Well, you can do a fairly quick check to make sure that the first
character is '+', '-', '.' (or other characters if you're parsing in an
international way) or a digit - and that all following characters are
either digits or '.' (or other separation characters, as before).

You could do it with a regular expression if you really wanted to, but
that would be slower than a hand-crafted routine.
 
F

Frank Oquendo

Paul said:
Does anyone know of any methods of testing strings to see
if they are numerical without converting the string?

double.TryParse()

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
M

Madhu [MVP]

Hi,

The best and fastest way to test the string is a decimal
is using RegEx.

The sample code snippet is given below,

string intValue = textBox1.Text;

Regex regex = new Regex (@"(^-?\d*\.?
\d*\d+\d*$)",RegexOptions.None);

Match match = regex.Match(intValue);

if (!match.Success)
{
MessageBox.Show("Invalid Decimal...");
}
else
MessageBox.Show("valid Decimal...");

Hope this helps...

Regards,
Madhu

MVP | MCSD.NET
 
J

Jon Skeet [C# MVP]

Madhu said:
The best and fastest way to test the string is a decimal
is using RegEx.

No it's not.

It's significantly faster to use a hand-crafted routine.

When parsing ints a while ago I got the following results (times) for
parsing some larger number (some valid, some invalid) of strings:

Straight exceptions: 00:01:15.7989936
Hard coded routine: 00:00:00.7010080
Double.TryParse: 00:00:40.8387232
Regular expressions: 00:00:43.0418912
IsNumeric from VisualBasic.Information: 00:01:06.9062064

That was for a very simple regular expression:
^[+-]?\d+$

While obviously the results would be *slightly* different for parsing
decimals, I doubt that they'd change enough to make regular expressions
faster than the hand-coded routine.
 
P

Paul K

Madhu,

I tried both the Regex class and using my own test
function -- both are the same speed with the current
amount of data that I have. I'll have to generate more
data to get a reliable reading.

Regardless of which is technically better, thank you for
your info. The component is now processing at an
acceptable speed.

Thanks!

Paul K
 
P

Paul K

Jon,

I've tried my own test function as well as the Regex
class -- both are the same speed with the current amount
of data I have. I'll have to generate more data to get a
reliable reading.

Regardless of which one is technically faster, thank you
for your info. The component is now processing at an
acceptable speed.

Thanks!

Paul K
 
P

Paul K

Frank,

I tried TryParse() -- it's a little too slow for my
component (with about 300 rows of data, there's about a
half-second performance hit between TryParse and using
either regular expressions or my own function).

Thanks anyway!

Paul K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top