Solved

regex to format numbers

Posted on 2013-06-11
16
431 Views
Last Modified: 2013-06-18
Hi
I Need a RegEx (replace) to Format a numeric User Input in the following

Input  "1"   --> Output "0,01"
Input  "11"   --> Output "0,11"
Input  "111"   --> Output "1,11"
Input  "1111"   --> Output "11,11"
Input  "11111"   --> Output "111,11"
Input  "111111"   --> Output "1.111,11"

and so on.

the number "1" is just an example, all numbers should be processed  

Pls no Answers with string.Format(...) or decimal.ToString(...)  or...

I need a Regular Expression !

var regex = new Regex(....);
string result = regex.Replace(userinput, ...);


Thank you
Peter
0
Comment
Question by:PeterInStingbert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39237622
What are the lower and upper bounds of your input?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39237639
Also, how are you going to use this? The conditional replacements you have above are going to make a single expression difficult to craft.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39237712
Another minor point of clarification.
does
"1.1" (note the decimal point) transform to "0,01.0,01" ?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:PeterInStingbert
ID: 39237733
Upper/Lower Bounds

Lower bound is one Digit

"1" --> "0,01"    

Upper bound are 7 digits

"9999999"  --> "99.999,99"


Condition - yes I know.
"Normal number formatting" with  decimal-point an thousands-separtor is easy.  

Is a regex with "Or-Condition" in input and replace-Condition generally possible?


I use a given module. This module run  
...
var regex = new Regex(....);
return result = regex.Replace(userinput, ...);

midmost.

Peter
0
 
LVL 1

Author Comment

by:PeterInStingbert
ID: 39237840
A decimal separator in the Input string has to keep

The decimal separator is "," (Germany) and is entered as ","

Input ",1" --> Output "0,10"
Input " 1,11" --> Output "1,11


thousands-separtor  "." are not in the input-string.

Peter
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39237929
Hmm.

"1" goes to "0,01" (ie divided by 100) BUT ",1" stays as "0,10" (not divided by 100)
0
 
LVL 1

Author Comment

by:PeterInStingbert
ID: 39238034
Yes
"1"  ist one Cent
but
",1" ist 0,1 Euro = 10 Cent

if a decimal Point is entered it is "Euro"
if not it is "Cent" = 1/100 "Euro"
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39238495
I don't think this is going to happen in one regex. What are you allowed to pass to this module? Just the regex, or the replacement pattern as well?
0
 
LVL 1

Author Comment

by:PeterInStingbert
ID: 39238530
I can pass:

 the regex-Expression = p1
 the regex-Options-Enum = p2
 the replacement-Pattern = p3


...
var regex = new Regex(p1, p2);
return result = regex.Replace(userinput, p3);
...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39238585
I suggest four calls:  Two to add zeroes to short numbers, one for the decimal portion, and one for the thousands separation.

e.g.

Module.DoReplacement(@"^(\d)$", RegexOptions.None, "00$1");
Module.DoReplacement(@"^(\d{2})$", RegexOptions.None, "0$1");
Module.DoReplacement(@"\d{2}$", RegexOptions.None, ",$1");
Module.DoReplacement(@"(?<=\d)(\d{3})(?=[^,]*,\d{2})", RegexOptions.RightToLeft, ".$1")

Open in new window


If you care to check the length of the source string before doing this logic, then you can just do the formatting in the first two calls (above) rather than letting them drop through to the other two calls.
0
 
LVL 1

Author Comment

by:PeterInStingbert
ID: 39239037
obviously there i no "universal" RegEx for oll my requirements   - ???

OK

@"^(\d)$", RegexOptions.None, "0,0$1"     is for Input  "1"   --> Output "0,01"


@"^(\d{2})$", RegexOptions.None, "0,$1"    is for Input  "11"   --> Output "0,11"


But what is @"(?<=\d)(\d{3})(?=[^,]*,\d{2})", RegexOptions.RightToLeft, ".$1") for?
I tried this Expression without any result


Peter
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39239155
The intent was that you pass the result of each call to the next replacement call. I don't know how your module is structured, and it doesn't appear that module accepts the source string as a parameter, so I pseudo-coded that bit. A bit more pseudo-code:

var result = Module.DoReplacement(source, @"^(\d)$", RegexOptions.None, "00$1");
result = Module.DoReplacement(result, @"^(\d{2})$", RegexOptions.None, "0$1");
result = Module.DoReplacement(result, @"\d{2}$", RegexOptions.None, ",$1");
result = Module.DoReplacement(result, @"(?<=\d)(\d{3})(?=[^,]*,\d{2})", RegexOptions.RightToLeft, ".$1")

return result;

Open in new window


You'll need to adapt the above to fit the environment, but it should at least demonstrate the concept.
0
 
LVL 1

Author Comment

by:PeterInStingbert
ID: 39240191
The module does not accept the Input value by Parameter.
The Input-Value "is handled inside" and is generated by Key-Commands.
The regex is just to generate a formatted view.
I know this is "not the best solution", but the modul it is given by 3rd Party
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39240808
If you can (effectively) only make one call to the module, then you are not going to be able to accomplish this formatting. If .NET supported conditional replacements for regex, then it could work, but the fact that you have numbers that you need to pad with leading zeroes complicates the issue.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 150 total points
ID: 39240875
I'd be tempted to make your own class that does some preformatting/checking then calls your module.
eg.  It checks if the numeric string requires a leading zero and prepends one if necessary.  It checks if it requires division be 100 and makes changes as required.  It checks if it will require thousands separators.  After all that you *might* have a string you can pass into your module just the once to obtain what you require.


ps.  All this might be simpler with a .Format or a .ToString call.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 39241925
If you do as Andy suggests, and you have your custom class perform the first three lines of what I posted, then you can use only the last line of my suggestion as your call to the module.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question