2010-06-24 1 views
2

내가 좋아하는 뭔가가있는 경우 : 전화 후C#에서 음수 금액을 허용하도록 통화 포맷터를 수정하려면 어떻게해야합니까?

Decimal moneyAmount = -1522; 

moneyAmount.toString("c"); 

그것은 같은 반환합니다

($ 1,522)

는 대신, 내가있을 소원을 음의 부호와 반대가 없다. 어떻게 내가 toString (에 보내 어떤 형식 공급자 수정할 수 있습니다) 그래서 나는 다음과 같은 효과를 얻을 수 있습니다

- $ 1,522

+0

가능한 중복 (" {0 : C2} ", -1234) (통화 형식)은 음수를 양수로 처리합니다.] (http://stackoverflow.com/questions/1001114/string-format0c2-1234-currency-format-treats-negative-numbers-as -posit) – Randolpho

답변

8

에서 촬영을 : 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); 
및 String.format [의
+0

죽은 링크. 0.0 솔루션은 작동하지만, –

+1

솔루션은 링크의 관련 콘텐츠의 99 %입니다. 다음은 페이지의 스냅 샷입니다. http://web.archive.org/web/20111214015527/http://keithdevens.com/weblog/archive/2007/Jun/04/C-sharp.currency – STW

관련 문제