?
Solved

Format currency question

Posted on 2006-03-26
9
Medium Priority
?
8,498 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

0
Comment
Question by:brdrok
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16297123
Hi brdrok,

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

Cheers!
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 2000 total points
ID: 16297265
Here is an example:

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

Expert Comment

by:Jase-Coder
ID: 16297800
even easier way to do this is declare a decimal variable and do

decimal Money = 50.5M

Console.WriteLine({0:C2}, Money);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Joni Kettunen
ID: 16298275
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
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16298583
>> 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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16299248
>>currency string would look different with 'YourVar.ToString("c")'

Don't you want to appear as the Control Panel is set?
0
 
LVL 4

Expert Comment

by:Joni Kettunen
ID: 16299787
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16299800
but if you don't specify the culture in your application, the formats of the control panel are used
0
 
LVL 7

Author Comment

by:brdrok
ID: 16299891
thanks for all the answers...but Expert1701 answer is a quick and dirty implementation of what I needed.  
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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