Subscribe to How-To Geek

Format a String as Currency in C#

When building a string for output to a web page, it’s useful to format any currency value in a human-friendly money format. This is extremely easy in C#.

The system format string works like this: {0:C}

For example, the following code example:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);

Outputs the following:

Order Total: $1,921.39

It’s worth noting that you must pass in a numeric value to the String.Format statement. If you pass in a string value, it won’t format correctly. If your currency value is in a string, you need to convert it into a double first.

| More
This article was originally written on 11/27/06 Tagged with: Programming

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (11)

  1. Justin

    You can also use:

    decimal moneyvalue = 1921.39m;
    string html = “Order Total: ” + moneyvalue.ToString(”C”);
    Console.WriteLine(html);

  2. Kingsley Magnus-Eweka

    how do i format the currency value i.e from $ to £ that isd from dollars to pounds using the string.format ?

  3. abdou

    I can give help I think look at control panel , regional option and look you can change your currency value there

  4. Soniya

    Ultimate one ..really helpful

  5. Barbaros Alp

    decimal moneyvalue = 1921.39m;

    You can use
    moneyvalue.ToString(”N”)
    instead of
    moneyvalue.ToString(”C”)

    by this way you get the formatted string just like currency, without Currency sign

  6. sameer

    by this way you get the formatted string just like currency, without Currency sign

    thanks i searching about this :)

  7. Joseph Marinaccio

    Thanks for a great article, direct and to the point!

    -Joseph Marinaccio
    Marinaccio Family Design

  8. Mehmet

    Thank you.

  9. Jeppe

    Is there a way to force the currency to be a specific one? Just because some has different regional settings it does not change the fact that a price is listed in (for example)pound.

  10. hk

    You can provide IFormatProvider. In this case you can do something like this

    using System.Globalization;

    decimal = moneyValue = 100.00m;
    string output = String.Format(CultureInfo.CurrentUICulture, “{0:C}”, moneyValue);

    With the above approach you don’t need to worry about culture specific format.

  11. jemala

    use this method
    public static string formatmoney(Decimal d)
    {

    return String.Format(CultureInfo.CreateSpecificCulture(”en-us”), “{0:C}”, d);
    }


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.