Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Format String to Currency

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joshua - 07 Sep 2007 16:15 GMT
I've seen many post regarding this, but I have yet to find what I
need.  I have a string that can contain numbers as well as text
(legacy data was poorly designed and I have to deal with it).  In the
instance that the string is a number, or decimal i would like to
format it as a currency otherwise I'd like to leave it as is.

So, the string could be (all of these are possible):
Type 1
Type A
123
123.33
123.00000003
Type 1 < 2

and I would like to format the ones that are numbers, so I'd like to
format it as such:
Type 1
Type A
$ 123.00
$ 123.33
$ 123.00
Type 1 < 2

Does anybody have any good tricks or something?  I guess I could do a
series of casts,  using the try catch option?   Any help would be
greatly appreciated.

Thank you.
Mark Rae [MVP] - 07 Sep 2007 16:28 GMT
> Does anybody have any good tricks or something?  I guess I could do a
> series of casts,  using the try catch option?   Any help would be
> greatly appreciated.

string strNumber = "<string to evaluate>";
decimal decTest;
if(Decimal.TryParse(strNumber, NumberStyles.Number, null, out decTest))
{
   strNumber = "$" + Convert.ToDecimal(strNumber).ToString("#,##0.00");
}

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Ghostman - 07 Sep 2007 16:35 GMT
I agree with Mark's post, except i'd change it to

string strNumber = "<string to evaluate>";
decimal decTest;
if(Decimal.TryParse(strNumber, NumberStyles.Number, null, out
decTest))
{
   strNumber = "$" + decTest.ToString("#,##0.00");

}

Unless there's a reason to convert it twice...
Mark Rae [MVP] - 07 Sep 2007 16:56 GMT
> Unless there's a reason to convert it twice...

None whatsoever - good suggestion...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.