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?
 
Expert1701Commented:
Here is an example:

  string number = "930.5";
  string formattedNumber = String.Format("{0:$#,##0.00}", Convert.ToDecimal(number));
0
 
Éric MoreauSenior .Net ConsultantCommented:
Hi brdrok,

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

Cheers!
0
 
Jase-CoderCommented:
even easier way to do this is declare a decimal variable and do

decimal Money = 50.5M

Console.WriteLine({0:C2}, Money);
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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.
0
 
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???
0
 
É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?
0
 
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/
0
 
Éric MoreauSenior .Net ConsultantCommented:
but if you don't specify the culture in your application, the formats of the control panel are used
0
 
brdrokAuthor Commented:
thanks for all the answers...but Expert1701 answer is a quick and dirty implementation of what I needed.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.