Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the value right of a . 2

Status
Not open for further replies.

mikeyd

Technical User
Jan 28, 2002
38
US
Hi,

if I got a number = 11.22 and trying with C# regular expressions to get just the 22 from the number.

Please anyone I am having big difficulties

TA
 
One method is to format in your code behind, for example:

txtA.Text = Int(1.667)

txtA shows: 1

..the other technique would be to format the textbox within the HTML code.



 
There's probablly a cleaner way to do it, but:

Regex rx = new Regex( @"\.\d\d" );
Match m = rx.Match( "567.99" );
String s = m.Value;
s = s.Remove( 0, 1 );
MessageBox.Show( s ); //for windows app... just know s is what you want
 
Boulder - the cleaner way is to use a zero-width look behind assertion. These assertions are cool - they allow you to stipulate that the dot must be present without actually capturing the dot.

In this case, the reg exp is (?<=\.)\d\d

the (?<=\.) bit means that the two digits must be preceeded by a dot.

Then you don't need to string manip to remove the dot.

HTH

Mark [openup]
 
I had a feeling you could do something like that. I'm pretty new to Regex, however. Star from me, too.
 
Couldn't you just use string functions instead of a regex?

Code:
string amount = "11.22";
string cents = amount.Substring (amount.IndexOf(".") + 1);


Keith
 
Yes of course, but a lot of the time regex is cleaner, performs better, and is more flexible.


Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top