Solved

# Currency Formay

Posted on 2006-05-25
285 Views
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
Question by:khyberman

LVL 6

Expert Comment

currency, like with a "\$", too?
long a = 2000000

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

LVL 6

Expert Comment

if you don't want the \$ you can use

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

LVL 6

Accepted Solution

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 6

Expert Comment

0

LVL 5

Expert Comment

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

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!