473,378 Members | 1,523 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,378 software developers and data experts.

converting strings to decimal

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
7 4568
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Newbee Adam | last post by:
I have been reading in help how I need to use decimal becuase currency does not exist like I used in vb6. I had a difficult time on google and msdn finding how or if I can take the value of text...
8
by: Dan | last post by:
In C#, how would you loop through each character in a string and convert them to their ascii values?
28
by: MLH | last post by:
The largest integer A97 can deal with is 2,147,483,647, as I understand it from HELP. I would be content to represent larger integers as strings. For example, "2147483648" would suit me fine. I...
1
by: Aaron Prohaska | last post by:
Good morning everyone, I'm having problems when I convert the SQL Server datatype smallmoney to the .net datatype decimal. I am doing this to pass a dollor amount to the Verisign payment...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
4
by: Jeff | last post by:
Hey ..NET 2.0 In my code am I trying to convert an int value into a decimal: decimal d = 0; int k = 87664; d = Convert.ToDecimal(k/100);
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.