2010-06-24 2 views
2

Si j'ai quelque chose comme:Comment puis-je modifier le formateur de devise pour autoriser les montants d'argent négatifs en C#?

Decimal moneyAmount = -1522; 

puis appelez

moneyAmount.toString("c"); 

Il retournera quelque chose comme:

(1.522 $)

, je souhaite au lieu qu'il y ait un signe négatif et pas de paraenthésis. Comment puis-je modifier ce format que je fournisseur envoie toString() pour que je puisse obtenir l'effet suivant:

- 1522 $

+0

double possible de [String.Format (» {0: C2} ", -1234) (format monétaire) traite les nombres négatifs comme positifs] (http://stackoverflow.com/questions/1001114/string-format0c2-1234-currency-format-treats-negative-numbers-as -posit) – Randolpho

Répondre

8

Extrait de: http://keithdevens.com/weblog/archive/2007/Jun/04/C-sharp.currency

// set currency format 
string curCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString(); 
System.Globalization.NumberFormatInfo currencyFormat = new System.Globalization.CultureInfo(curCulture).NumberFormat; 
currencyFormat.CurrencyNegativePattern = 1; 

number.ToString("c", currencyFormat); 
// or string.Format(currencyFormat, "{0:c}", number); 
+0

Lien mort. 0.0 La solution fonctionne, bien que –

+1

La solution était 99% du contenu pertinent du lien. Voici un aperçu de la page: http://web.archive.org/web/20111214015527/http://keithdevens.com/weblog/archive/2007/Jun/04/C-sharp.currency – STW

Questions connexes