Subscribe to How-To Geek Mag-subscribe sa Paano-Upang Geek

Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

Format a String as Currency in C# Format ng isang String bilang Currency sa 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. Kapag ang gusali ng isang string para sa output sa isang web page, ito ay kapaki-pakinabang sa format ng anumang halaga ng pera sa isang tao-friendly ng pera na format. This is extremely easy in C#. Ito ay lubhang madali sa C #.

The system format string works like this: {0:C} Ang sistema ng string ng format ng mga gawa tulad nito: (0: C)

For example, the following code example: Halimbawa, ang sumusunod na halimbawa ang code:

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

Outputs the following: Outputs ang mga sumusunod:

Order Total: $1,921.39 Order Total: $ 1,921.39

It's worth noting that you must pass in a numeric value to the String.Format statement. Ito ay nagkakahalaga ng pagpuna na dapat mong ipasa sa isang numerong halaga sa pahayag String.Format. If you pass in a string value, it won't format correctly. Kung pumasa ka sa isang string na halaga, ito ay hindi na format ng tama. If your currency value is in a string, you need to convert it into a double first. Kung ang iyong halaga ng pera ay isang string, kailangan mong i-convert ito sa isang double muna.

This article was originally written on 11/27/06 Tagged with: Ang artikulong ito ay orihinal na isinulat sa 11/27/06 Tagged with: Programming Programming

Daily Email Updates Araw-araw na Updates Email

You can get our how-to articles in your inbox each day for free. Maaari kang makakuha ng aming kung-paano na mga artikulo sa iyong inbox sa bawat araw para sa libre. Just enter your name and email below: Ilagay lamang ang inyong pangalan at email sa ibaba:


Name: Pangalan:
Email: Email:

Comments (11) Comments (11)

  1. Justin Justin

    You can also use: Maaari din ninyong gamitin:

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

  2. Kingsley Magnus-Eweka Kingsley Magnus-Eweka

    how do i format the currency value ie from $ to £ that isd from dollars to pounds using the string.format ? paano ko i-format ang pera ibig sabihin na halaga mula sa $ £ na isd dolyar mula sa pounds gamit ang string.format?

  3. abdou abdou

    I can give help I think look at control panel , regional option and look you can change your currency value there Maaari ko bang magbigay ng tulong sa tingin ko ay tumingin sa control panel, rehiyonal na opsiyon at hanapin maaari mong palitan ang iyong pera may halaga

  4. Soniya Soniya

    Ultimate one ..really helpful Ultimate isa .. talagang helpful

  5. Barbaros Alp Barbaros Alp

    decimal moneyvalue = 1921.39m; decimal moneyvalue = 1921.39m;

    You can use Maaari mong gamitin ang
    moneyvalue.ToString(”N”) moneyvalue.ToString ( "N")
    instead of sa halip ng
    moneyvalue.ToString(”C”) moneyvalue.ToString ( "C")

    by this way you get the formatted string just like currency, without Currency sign sa pamamagitan ng ganitong paraan makuha ninyo ang mga format na string tulad ng pera, walang mag-sign Currency

  6. sameer sameer

    by this way you get the formatted string just like currency, without Currency sign sa pamamagitan ng ganitong paraan makuha ninyo ang mga format na string tulad ng pera, walang mag-sign Currency

    thanks i searching about this thanks ako tungkol sa paghahanap na ito :)

  7. Joseph Marinaccio Joseph Marinaccio

    Thanks for a great article, direct and to the point! Salamat sa isang mahusay na artikulo, direktang at sa punto!

    -Joseph Marinaccio -Joseph Marinaccio
    Marinaccio Family Design Marinaccio Family Design

  8. Mehmet Mehmet

    Thank you. Salamat.

  9. Jeppe Jeppe

    Is there a way to force the currency to be a specific one? Mayroon bang paraan upang pilitin ang pera ay isang tiyak na isa? Just because some has different regional settings it does not change the fact that a price is listed in (for example)pound. Just dahil ang ilan ay may iba't-ibang rehiyon na setting na ito ay hindi baguhin ang katunayan na ang presyo ay nakalista sa (halimbawa) pound.

  10. hk HK

    You can provide IFormatProvider. Maaari kang magbigay ng IFormatProvider. In this case you can do something like this Sa kasong ito maaari mong gawin sa isang bagay tulad nito

    using System.Globalization; gamit System.Globalization;

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

    With the above approach you don't need to worry about culture specific format. Sa itaas paraan hindi mo na kailangan mag-alala tungkol sa kultura ng mga tiyak na format.

  11. jemala jemala

    use this method gamitin ang pamamaraan na ito
    public static string formatmoney(Decimal d) public static string formatmoney (Desimal d)
    { (

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


Leave a Comment Iwanan ng isang Puna




Leave your Iwanan ang iyong mga friendly friendly comment here. puna dito.

If you have a computer help question, Kung ikaw ay may isang computer ng tulong tanong, click here to leave it on the forums i-click dito upang mag-iwan ito sa forums instead. sa halip ng.

Note: Your comment may not show up immediately on the site. Tandaan: Ang iyong puna ay maaaring hindi magpapakita agad sa site.

Our Friends Ang aming mga Friends
Getting Started Pagsisimula


About How-To Geek Tungkol sa Paano-Upang Geek
What Is That Process? Ano ba ang Proseso Iyon?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
mDNSResponder.exe mDNSResponder.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

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