Regular Expression for currency

  • Thread starter Thread starter hellbent4u
  • Start date Start date
H

hellbent4u

Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur
 
Ankur,

Do you have to use a regular expression here? It seems like you have to
use a number, so why not use the TryParse method on the Int32 class? You
can set it to recognize thousands separators, or skip them (using the
NumberFormatInfo class) and then go from there.

Hope this helps.
 
Hi,


The ugliest code ever:

try
{
double d = Convert.toDouble( mycurvalue.Replace( "$", "").Replace(
",","") );

}

Hope you like it :)
 
Ankur,

Do you have to use a regular expression here? It seems like you have to
use a number, so why not use the TryParse method on the Int32 class? You
can set it to recognize thousands separators, or skip them (using the
NumberFormatInfo class) and then go from there.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I need a regular expression to be used in an ASP page which would
allow a user to enter:
1000 as well as
1,000
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
If anyone's aware, please let me know. I am in urgent need to use it.
Thanks and Regards,
Ankur- Hide quoted text -

- Show quoted text -

Hi again,

Thanks for your response. But I actually need to use regular
expression on aspx page itself so that the user if wishes to enter an
amount with thousand separator, he is allowed to do so. Also the
simple form(not having commas). Please help me with this if you can.

Regards,
Ankur
 
Thanks for your response. But I actually need to use regular
expression on aspx page itself so that the user if wishes to enter an
amount with thousand separator, he is allowed to do so. Also the
simple form(not having commas). Please help me with this if you can.

And if you will have to localize your web site, or accept money from outside
US, you will run into troubles (because the decimal separator is comma in
most of Europe). TryParse is the safest option.
 
Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal separator.
 
I need aregularexpressionto be used in an ASP page which would
allow a user to enter:
1000 as well as
1,000
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal separator.




If anyone's aware, please let me know. I am in urgent need to use it.
Thanks and Regards,
Ankur- Hide quoted text -

- Show quoted text -



Hi Ben,

I tried your regular expression. It worked for most of the cases. But
in addition to all, it also accepts 1000,000 which it should not have.
Instead 1,000,000 is what should have been allowed. Any minor change
that can be done to the reg-ex you provided??

Secondly, I never posted my efforts on this thread. Here's what I have
been trying my hands on :

((\d{1,3})(\,\d{3})*)|(\d\d*))((\.\d{2})?)

It's an OR of two reg-ex, first one being the reg-ex to recognise the
number with thousand separator and second without thousand separator.
The pipe "|" doesn't seem to OR the two conditions. Any idea why is
this happening?

Hoping to get some headway..

Regards,
Ankur
 
Try the following:

\b(?:\d{1,3}(?(?=[,.])[,.](?:\d{3}(?:[.,](?=\d)|\b)|(?:\b|\d{1,2}))|\b))+

It's a bit complicated to explain, but I'll try.

First, it asserts that the match must begin on a word boundary. The match
follows as a non-capturing group which may be repeated any number of times.
The match consists of:

1. 1-3 digits
2. These digits may be followed by a period or comma.
a. If they are followed by a period or a comma, they are followed by
either:
aa. Exactly 3 digits followed by a period or comma that must be
followed by a (not captured) digit, or
bb. a word boundary
b. Otherwise (not followed by a period or comma) either
aa. 1 or 2 digits, or
bb. a word boundary
3. If the initial 1-3 digits are NOT followed by a period or comma, they
must
be followed by a word boundary.

So, essentially, the rules expressed are that a match consists of a sequence
of 1-3 digits with commas between the groups of digits. However, if a group
of digits is surrounded by periods or commas, there must be exactly 3.

The trick is the quantifier on the entire group. This creates a repeating
pattern of groups of 1-3 digits followed by periods or commas. However, the
last group of digits must be followed by a word boundary, and the first
group must be preceded by a word boundary. And if a group of digits is
between periods/commas, there must be exactly 3.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

I need aregularexpressionto be used in an ASP page which would
allow a user to enter:
1000 as well as
1,000
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal
separator.




If anyone's aware, please let me know. I am in urgent need to use it.
Thanks and Regards,
Ankur- Hide quoted text -

- Show quoted text -



Hi Ben,

I tried your regular expression. It worked for most of the cases. But
in addition to all, it also accepts 1000,000 which it should not have.
Instead 1,000,000 is what should have been allowed. Any minor change
that can be done to the reg-ex you provided??

Secondly, I never posted my efforts on this thread. Here's what I have
been trying my hands on :

((\d{1,3})(\,\d{3})*)|(\d\d*))((\.\d{2})?)

It's an OR of two reg-ex, first one being the reg-ex to recognise the
number with thousand separator and second without thousand separator.
The pipe "|" doesn't seem to OR the two conditions. Any idea why is
this happening?

Hoping to get some headway..

Regards,
Ankur
 

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

Back
Top