views:

3091

answers:

6

Hi,

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 teh textbox to achieve the above formatting?

I tried this, without success:

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

Many Thanks,

+1  A: 

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.

Jon Skeet
+6  A: 
string forDisplay = currentBalance.ToString("N2");
LukeH
A: 

.Replace(currencysign, "")

salietata
+1  A: 

Have you tried:

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

This is the long-hand equivalent of:

currentBalance.ToString("N2");
Garry Shutler
A: 
        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
Prasanna
A: 

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

webKite