Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a double value like currency but without the currency sign (C#)

I feed a textbox a string value showing me a balance that need to be formatted like this:

###,###,###,##0.00 

I could use the value.ToString("c"), but this would put the currency sign in front of it.

Any idea how I would manipulate the string before feeding the textbox to achieve the above formatting?

I tried this, without success:

String.Format("###,###,###,##0.00", currentBalance); 

Many Thanks,

like image 590
Houman Avatar asked Jun 26 '09 11:06

Houman


People also ask

How do you format a currency without a dollar sign?

You can remove the dollar sign from currency formatting in Excel 2013 by right-clicking a cell, selecting Format Cells, then clicking the Symbol dropdown menu and choosing the None option.

How will you format a value to currency?

On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.


2 Answers

If the currency formatting gives you exactly what you want, clone a NumberFormatInfo with and set the CurrencySymbol property to "". You should check that it handles negative numbers in the way that you want as well, of course.

For example:

using System; using System.Globalization;  class Test {     static void Main()     {         NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;         nfi = (NumberFormatInfo) nfi.Clone();          Console.WriteLine(string.Format(nfi, "{0:c}", 123.45m));         nfi.CurrencySymbol = "";         Console.WriteLine(string.Format(nfi, "{0:c}", 123.45m));     } } 

The other option is to use a custom numeric format string of course - it depends whether you really want to mirror exactly how a currency would look, just without the symbol, or control the exact positioning of digits.

like image 160
Jon Skeet Avatar answered Sep 16 '22 15:09

Jon Skeet


string forDisplay = currentBalance.ToString("N2"); 
like image 30
LukeH Avatar answered Sep 18 '22 15:09

LukeH