 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 29th, 2006, 11:53 PM
|
|
Registered User
|
|
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Urgent Help Need regarding Regular Expression
Hi All!!
This is my first post here so hope you people will help me.
I need to format the price in the text field on the leave event of a text box in windows form of c# 2.0, Mask text field wont help as it requires an exact format and I wont know what the user will enter.
A few examples of what I want:
i) 2344534.238 --> $2,344,534.24 means it put a comma after every 3 digits to the left of decimal point (Thousands separator) and rounds off the digits to the right of decimal point, if they are more than 2.
ii) 342354 --> $342,354.00 Put the decimal point and 00 at the end of the number if the user doesn't give the decimal point himself.
iii) 345348.9 --> $345,348.90 If the user writes only one digit to the right of decimal point it appends 0 to it.
and offcourse all have a $ sign appened at their start.
I know very little about regular expressions, so if any one of you can make a regular expression for this formatting, which automatically achieves this and give a snippet of code of how it works, I'd be really really grateful. If anyone of you can give me just a regular expression that puts the commas as the thousands separator that'd be fine as well, because I've managed the other formatting manually, a full reg exp. would be a bonus:).
Regards,
Sajid.
|
|

September 30th, 2006, 07:34 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sajid,
Here are two routines, liberally commented, that should accomplish what you want. Hope it helps...
Code:
private void textBox1_Leave(object sender, EventArgs e)
{
// Will receive the properly formatted dollars and cents string.
string dollarsAndCents = "";
// Validate the text string input. If the string is in a valid
// format, then convert it to dollars and cents format. Finally,
// replace the original string in the textbox with the newly
// formatted dollars and cents string.
if (this.ToCurrency(textBox1.Text, ref dollarsAndCents))
textBox1.Text = dollarsAndCents;
}
private bool ToCurrency(string text, ref string dollarsAndCents)
{
// Perform some preliminary formatting on the original text string.
// Specifically, delete any occurrances of the dollar sign ($),
// commas (,) and blank spaces ( ).
text = text.Replace("$", "").Replace(",", "").Replace(" ", "");
// Formulate our regular expression pattern. It includes one
// overriding group called currency. Within the currency group,
// we can have three distinct patterns:
//
// Dollars.Cents
// Dollars.
// .Cents
//
// The decimal point is mandatory in the "Dollars.Cents", and the
// ".Cents" variants, but is optional in the "Dollars." variant.
//
// Once the pattern is formulated, attempt a regular expression
// match using the text supplied in the textbox.
string pattern = @"^\s*(?<Currency>(?i:(?<Dollars>\d+)\.(?<Cents>\d{1,2}))|(?<Dollars>\d+)\.*|\.(?<Cents>\d{1,2}))";
Match m = Regex.Match(text, pattern);
// If our pattern match was unsuccessful, then the string is not
// in a valid format.
if (!m.Success)
return false;
// Now that we've validated the input text, let's format it
// into a proper currency format. First, use a string builder
// object to concatenate the dollars portion with a decimal
// point and finally with the cents portion.
StringBuilder sb = new StringBuilder();
if (m.Groups["Dollars"].Success)
sb.Append(m.Groups["Dollars"].Value);
sb.Append(".");
if (m.Groups["Cents"].Success)
sb.Append(m.Groups["Cents"].Value);
// Now that we have the dollars, decimal and cents concatenated,
// let's convert the concatenated string into value of type
// "double", and then use the .NET framework's own formatting
// tools to convert it into currency format.
dollarsAndCents = string.Format("{0:C}", Convert.ToDouble(sb.ToString()));
// Success.
return true;
}
- Roger Nedel
|
|

October 1st, 2006, 05:29 AM
|
|
Registered User
|
|
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Alot Roger,
It worked perfectly.
Regards,
Sajid
|
|

October 1st, 2006, 11:57 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sajid,
Just realized something. Your examples showed that data with more than 2 digits in the cent's portion should round. My suggested solution doesn't round. Rather, it truncates. Just want you to be aware of that in case it's critical. If you want it to round instead, I suggest you change the pattern string from:
Code:
string pattern = @"^\s*(?<Currency>(?i:(?<Dollars>\d+)\.(?<Cents>\d{1,2}))|(?<Dollars>\d+)\.*|\.(?<Cents>\d{1,2}))";
to
Code:
string pattern = @"^\s*(?<Currency>(?i:(?<Dollars>\d+)\.(?<Cents>\d+))|(?<Dollars>\d+)\.*|\.(?<Cents>\d+))";
That should solve the rounding issue (if it is an issue at all).
Cheers.
- Roger Nedel
|
|
 |