473,699 Members | 2,734 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 4594
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
3191
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 squigglies on the line. But is this actually converting it to decimal. Next if so, I do not like the...
8
48268
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
4809
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 and I would be happy to represent it as a string, as it would suit my purposes fine. Since the...
1
3771
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 when I use 250.50 the decimal datatype drops the last zero making this 250.5 which causes the...
116
35880
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 haywire. So my question is, given this code: int main() { float f = 59.89F;
3
2395
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 etc., not just a simple "this number of digits (or more if needed) before the decimal point
21
2010
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 for further processing and efficient storage... Example row from sampled file data: , ....]
4
3451
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
5111
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
8704
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8894
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7777
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2015
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.