473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ToDecim al 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 4581
Paul K <an*******@disc ussions.microso ft.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.ToDecim al 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.co m>
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*$)",Re gexOptions.None );

Match match = regex.Match(int Value);

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.ToDecim al or
decimal.Pars e 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*******@disc ussions.microso ft.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.798993 6
Hard coded routine: 00:00:00.701008 0
Double.TryParse : 00:00:40.838723 2
Regular expressions: 00:00:43.041891 2
IsNumeric from VisualBasic.Inf ormation: 00:01:06.906206 4

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.co m>
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*$)",R egexOptions.Non e);

Match match = regex.Match(int Value);

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.ToDecim al or
decimal.Par se 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*******@disc ussions.microso ft.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.ToDecim al 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 aninternationa l 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.co m>
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.TryPars e()

--
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
3181
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 box as decimal or do I just have to make another decimal variable .. So I took a guess hoping CDec would come up blue if I type it in with no...
8
48251
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
4787
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 would be interested in converting larger HEX numbers to decimal integer equivalents. For example, a 16-digit HEX number is quite a large decimal value...
1
3764
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 gateway. Verisign requires that the amount passed in is in the format ###.##, so if I have 250.50 its passed in exactly as that. What is happening is that...
116
35709
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going...
3
2383
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), and - trailing zeroes are kept. This means I need it to be converted using the scientific notation if the number is greater than a certain value...
21
1979
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 attributes (colls in a data base/ csv file, eg. height weight income etc.) to determine the most efficient 'type' to convert the attribute coll into...
4
3446
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
5086
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 amount = 126.60; double interestRate = .075; double interest = amount * interestRate;
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.