Search code examples
c#asp.netstring-formattingcurrencymoney-format

String.Format Same Code Different View


I have a code like this;

GridView1.FooterRow.Cells[11].Text = String.Format("{0:c}", sumKV)

In my computer this code gives a result like that;

enter image description here

But when I upload this code to my virtual machine it looks like this;

enter image description here

TL means Turkish Liras. But I don't want to show the currency. I just want numbers.

I also don't want to change the formating of numbers. (Like 257.579,02)

How can I only delete TL in this code?


Solution

  • I would use this:

    var cultureWithoutCurrencySymbol = 
        (CultureInfo)CultureInfo.CurrentCulture.Clone();
    cultureWithoutCurrencySymbol.NumberFormat.CurrencySymbol = "";
    GridView1.FooterRow.Cells[11].Text = 
                String.Format(cultureWithoutCurrencySymbol, "{0:c}", sumKV).Trim();
    

    Background:
    This will still keep the currency formatting for the current culture, it just removes the currency symbol.
    You can save this special culture somewhere, so you don't have to create it every time you need to format your values.

    UPDATE:

    • Now it even compiles... ;-)
    • Added a Trim(), because there is still a space after the formated number.