By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

converting strings to decimal

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Paul K <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Paul K wrote:
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)
Nov 15 '05 #3

P: n/a
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
-----Original Message-----
I'm writing a small component that needs to be as fast aspossible. The component needs to convert a string to
decimal during the course of it's processing. However, Ineed 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 seeif they are numerical without converting the string?

Paul K
.

Nov 15 '05 #4

P: n/a
Madhu [MVP] <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
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
-----Original Message-----
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
-----Original Message-----
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
.

.

Nov 15 '05 #6

P: n/a
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
-----Original Message-----
Paul K <an*******@discussions.microsoft.com> wrote:
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 firstcharacter is '+', '-', '.' (or other characters if you're parsing in aninternational way) or a digit - and that all following characters areeither digits or '.' (or other separation characters, as before).
You could do it with a regular expression if you really wanted to, butthat would be slower than a hand-crafted routine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #7

P: n/a
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
-----Original Message-----
Paul K wrote:
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 whodon't.

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

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.