We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Format currency question

brdrok
brdrok asked
on
Medium Priority
8,555 Views
Last Modified: 2012-05-05
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

Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Hi brdrok,

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

Cheers!
Top Expert 2006
Commented:
Here is an example:

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
even easier way to do this is declare a decimal variable and do

decimal Money = 50.5M

Console.WriteLine({0:C2}, Money);
Joni KettunenSenior Software Engineer

Commented:
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.
>> 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 Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>currency string would look different with 'YourVar.ToString("c")'

Don't you want to appear as the Control Panel is set?
Joni KettunenSenior Software Engineer

Commented:
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 Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
but if you don't specify the culture in your application, the formats of the control panel are used

Author

Commented:
thanks for all the answers...but Expert1701 answer is a quick and dirty implementation of what I needed.  
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.