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

C#StringFormatCurrency

C# Problem Overview


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,

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

string forDisplay = currentBalance.ToString("N2");

Solution 3 - C#

Have you tried:

currentBalance.ToString("#,##0.00");

This is the long-hand equivalent of:

currentBalance.ToString("N2");

Solution 4 - C#

string result=string.Format("{0:N2}", value); //For result like ### ### ##.##

Solution 5 - C#

You can do this with the group separator and the section separator, like this:

currentBalance.ToString("#,0.00;(#,0.00)");

This does not account for culture variances like the answer from @JonSkeet would, but this does mimic decimal place, rounding, thousands separation, and negative number handling that en-US culture currency format produces using a single custom format string.

.NET Fiddle Demo

Solution 6 - C#

var result = currentBalance.ToString("C").Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol, "");

Solution 7 - C#

CultureInfo cultureInfo = new CultureInfo("en-US");
cultureInfo.NumberFormat.CurrencySymbol = "Rs.";

Thread.CurrentThread.CurrentCulture = cultureInfo;
decimal devimalValue = 3.45M;
this.Text = devimalValue.ToString("C2"); //Rs.3.45

Solution 8 - C#

This may be overkill, but it rounds, formats...

@helper TwoDecimalPlaces(decimal? val)
{
    decimal x = 0;
    decimal y = 0;
    string clas = "text-danger";
    if (val.HasValue)
    {
	    x = (decimal)val;
	    if (val > 0)
	    {
		    clas = "";
	    }
    }
    y = System.Math.Round(x, 2);
    IFormatProvider formatProvider = new System.Globalization.CultureInfo(string.Empty);
    <span class="@clas">@string.Format("{0:N2}", y)</span>
}

Solution 9 - C#

This simple solution works for me with US currency.

If not needing international currency support use this and replace the $ with the currency symbol(s) to be removed:

// for USD
string result = currentBalance.ToString("C").Replace("$", "")

or

// for EUR
string result = currentBalance.ToString("C").Replace("€", "")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHoumanView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#LukeHView Answer on Stackoverflow
Solution 3 - C#Garry ShutlerView Answer on Stackoverflow
Solution 4 - C#webKiteView Answer on Stackoverflow
Solution 5 - C#Jordan ParkerView Answer on Stackoverflow
Solution 6 - C#Felipe JostView Answer on Stackoverflow
Solution 7 - C#PrasannaView Answer on Stackoverflow
Solution 8 - C#HerGizView Answer on Stackoverflow
Solution 9 - C#Taylor BrownView Answer on Stackoverflow