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 de un şir ca monedă în 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. Când construieşte un şir de ieşire la o pagină de web, este util să format de orice monedă valoare într-un om format de bani prietenos. This is extremely easy in C#. Acest lucru este extrem de uşor în C #.

The system format string works like this: {0:C} String format de sistemul funcţionează astfel: (0: C)

For example, the following code example: De exemplu, următorul exemplu de cod:

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

Outputs the following: Rezultate următoarele:

Order Total: $1,921.39 Comanda total: $ 1,921.39

It's worth noting that you must pass in a numeric value to the String.Format statement. Este demn de remarcat faptul că trebuie să treacă într-o valoare numerică în vedere declaraţia String.Format. If you pass in a string value, it won't format correctly. Dacă aţi trece într-o valoare şir de caractere, acesta nu va formatul corect. If your currency value is in a string, you need to convert it into a double first. În cazul în care valoarea în moneda dumneavoastră este într-un şir, trebuie să îl transforme într-o prima dubla.

This article was originally written on 11/27/06 Tagged with: Acest articol a fost scris iniţial pe 11.27.06 Etichetate cu: Programming De programare

Daily Email Updates Daily Actualizări de email

You can get our how-to articles in your inbox each day for free. Aveţi posibilitatea să obţineţi modul nostru de-a articole în Inbox în fiecare zi pentru drum liber. Just enter your name and email below: Doar introduceţi numele dvs. şi e-mail de mai jos:


Name: Nume:
Email: E-mail:

Comments (11) Comentarii (11)

  1. Justin Iustin

    You can also use: Puteţi folosi, de asemenea:

    decimal moneyvalue = 1921.39m; moneyvalue zecimală = 1921.39m;
    string html = “Order Total: ” + moneyvalue.ToString(”C”); html string = "Ordine 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 ? cum a face i formatul de moneda in care este valoarea de la $ pentru a £ faptul că de la DSI de dolari pentru a lire sterline folosind 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 Eu ii pot oferi ajutor cred ca uita-te la panoul de control, opţiunea regională şi căutaţi puteţi schimba valoarea de moneda de acolo

  4. Soniya Soniya

    Ultimate one ..really helpful Ultimate o .. într-adevãr ajutor

  5. Barbaros Alp Barbaros Alp

    decimal moneyvalue = 1921.39m; moneyvalue zecimală = 1921.39m;

    You can use Aveţi posibilitatea să utilizaţi
    moneyvalue.ToString(”N”) moneyvalue.ToString ( "N")
    instead of în loc de
    moneyvalue.ToString(”C”) moneyvalue.ToString ( "C")

    by this way you get the formatted string just like currency, without Currency sign de acest fel veţi obţine şirul de formatat la fel ca şi valută, fără semn de valuta

  6. sameer sameer

    by this way you get the formatted string just like currency, without Currency sign de acest fel veţi obţine şirul de formatat la fel ca şi valută, fără semn de valuta

    thanks i searching about this Am mulþumiri căutarea despre acest :)

  7. Joseph Marinaccio Joseph Marinaccio

    Thanks for a great article, direct and to the point! Multumesc pentru o mare de articol, direct şi la obiect!

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

  8. Mehmet Mehmed

    Thank you. Mulţumesc.

  9. Jeppe Jeppe

    Is there a way to force the currency to be a specific one? Există o modalitate de a forţa moneda a fi unul special? Just because some has different regional settings it does not change the fact that a price is listed in (for example)pound. Doar pentru ca unele are diferite setări regionale, aceasta nu schimbă faptul că un preţ este enumerate la litera (de exemplu) lire.

  10. hk HK

    You can provide IFormatProvider. Poti oferi IFormatProvider. In this case you can do something like this În acest caz, poţi să faci ceva de genul asta

    using System.Globalization; System.Globalization folosind;

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

    With the above approach you don't need to worry about culture specific format. Cu abordarea de mai sus nu trebuie să vă faceţi griji despre cultura format specific.

  11. jemala jemala

    use this method utilizaţi această metodă
    public static string formatmoney(Decimal d) publice formatmoney static string (Decimal d)
    { (

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


Leave a Comment Lăsaţi un comentariu




Leave your Leave tău friendly prietenoasa comment here. comentariu aici.

If you have a computer help question, Dacă aveţi un calculator ajuta la întrebare, click here to leave it on the forums click aici pentru a lăsa pe forum instead. în schimb.

Note: Your comment may not show up immediately on the site. Notă: Comentariul dvs. să nu apară imediat pe site-ul.

Our Friends Our Friends
Getting Started Noţiuni de bază


About How-To Geek Despre "Cum să" Geek
What Is That Process? Ce este acest proces?
svchost.exe svchost.exe
jusched.exe winampa.exe
dwm.exe dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.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. Toate drepturile rezervate.