Format currency question

Hi,

Need help with how to display a number (actually it's a string) in a nice dollar format.  For example, there are dollar values that look something like this:

930.5
110
552.3333
87.66667
what I would like to do is to have those number look like dollar number:

$930.50
$110.00
$552.33
$87.67 or 87.66.

thanks

LVL 7
brdrokAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
Hi brdrok,

Isn't it something like : YourVar.ToString("c")

Cheers!
Expert1701Commented:
Here is an example:

  string number = "930.5";
  string formattedNumber = String.Format("{0:$#,##0.00}", Convert.ToDecimal(number));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jase-CoderCommented:
even easier way to do this is declare a decimal variable and do

decimal Money = 50.5M

Console.WriteLine({0:C2}, Money);
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Joni KettunenSenior Software EngineerCommented:
Here is an example that works world wide. I fine tuned Expert1701's example:

string number = "930.5";

// Get current number format to avoid defining gazillion properties
System.Globalization.NumberFormatInfo nf = null;
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
nf = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
// Define decimal separator (according to input)
nf.NumberDecimalSeparator = ".";

string formattedNumber = String.Format("{0:$#,##0.00}", Convert.ToDecimal(number, nf));

emoreau's and Jase-Coder's examples are completely ingorant about current culture and Expert1701 needs some fine tuning in his code as well. Here is an example that defines number separator for input.

If your solution should work on different cultures (like in US and Great Britain) currency string would look different with 'YourVar.ToString("c")' type conversions. If you really want to get local currency to be shown this is the way to do it. But if you need to show for example how much each oil barrel costs in US dollars, you have to use something like Expert1701 shows.

Depending on input data, you might have to fine tune input reading. My current decimal separator (I live in Europe) is ',' character so sometimes it is important to define decimal separator according to input data.
Jase-CoderCommented:
>> emoreau's and Jase-Coder's examples are completely ingorant about current culture and Expert1701 needs some fine tuning in his code as well. Here is an example that defines number separator for input.

How???
Éric MoreauSenior .Net ConsultantCommented:
>>currency string would look different with 'YourVar.ToString("c")'

Don't you want to appear as the Control Panel is set?
Joni KettunenSenior Software EngineerCommented:
I'll improve my answer, with more comments and better implementation:

using System.Globalization;

string number = "930.5";
// Get number formatting by US culture
CultureInfo usCulture = CultureInfo.CreateSpecificCulture("en-US");
// Create format provider
NumberFormatInfo nfUs = usCulture.NumberFormat;
// Format given input to decimal number and show it as US currency.
string formattedNumber = Convert.ToDecimal(number, nfUs).ToString("c", nfUs);

Note that culture is defined twice. Once when number is parsed and second time when number is formatted to currency ("c").

See also quick reference in string formatting with C#:
http://blog.stevex.net/index.php/string-formatting-in-csharp/
Éric MoreauSenior .Net ConsultantCommented:
but if you don't specify the culture in your application, the formats of the control panel are used
brdrokAuthor Commented:
thanks for all the answers...but Expert1701 answer is a quick and dirty implementation of what I needed.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.