?
Solved

Currency Formay

Posted on 2006-05-25
5
Medium Priority
?
287 Views
Last Modified: 2010-05-18
How can i convert a long into a string with currency format, for example

long a = 2000000

 i ould like to print:
2,000,000


Thanks,






0
Comment
Question by:khyberman
  • 3
5 Comments
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16763618
currency, like with a "$", too?
long a = 2000000

string curr = String.Format("{0:c}", a);
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16763677
if you don't want the $ you can use

string curr = String.Format("{0:N}", a));
0
 
LVL 6

Accepted Solution

by:
PoeticAudio earned 1000 total points
ID: 16764121
oops just noticed syntax error on my last post

string curr = String.Format("{0:N}", a));

should be

string curr = String.Format("{0:N}", a);
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 16780708
I would suggest you make a Currency or Money class to hide the conversion implementation to make your code more flexible and cleaner.  For example you can do something like this:

Money m = new Money(2000000);
Console.Writeline(m);  // You can output any where, like in a Form or something, it doesn't have to be console
// output would be $200,000

I would also overload the +,-,*,/ and == operators, so you can do cool things like:

Money m1 = new Money(5000);
Money m2 = new Money(5000);
Money m3 = m1 + m2 ;
Console.Writeline(m3);  // this will output $10,000

Basically you have a constructor that takes in a long or double, and you'll have to override the ToString() method.  In the ToString() method you'll basically call the String.Format().

This way, you don't have to litter String.Format every where in your code.  If you decide to change how the Currency is formated, you can just change the Money object itself, everything will work.  Otherwise you'll have to hunt down the places you did String.Format().

The Money class I shown can be a struct if you like, but it's fine being an object.

Cheers,

Aaron
http://aaronfeng.blogspot.com/
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 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