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.

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:



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);
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?
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
Ultimate one ..really helpful Ultimate o .. într-adevãr ajutor
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
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
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
Thank you. Mulţumesc.
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.
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.
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);
} )