KBD

Keith Devens .com

Friday, October 10, 2008 Flag waving
We ought always to deal justly, not only with those who are just to us, but likewise to those who... – Hierocles
← Worldofraids forums :: Sha'tari Skyguard Reputation GuideFIREBUG IS SO AWESOME →

Daily link icon Monday, June 4, 2007

How to "correctly" format currency in C#

For some reason C#, by default, formats negative currency as "($12,345,678.90)" instead of "-$12,345...". Here's code that lets you change that (wordwrapped for her pleasure):

// 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);

C# is retarded.

← Worldofraids forums :: Sha'tari Skyguard Reputation GuideFIREBUG IS SO AWESOME →

Comments XML gif

Revence 27 (http://freeshells.ch/~revence) wrote:

Um ... that's more like the libraries being retarded, rather that C# being retarded. But then, with them money issues, one will almost always be dealing with a different library for a different day.
If C# is retarded, it is because she inherited bad genes.

∴ Revence 27 | 4-Jun-2007 2:15pm est | http://freeshells.ch/~revence | #10123

Nathaniel wrote:

In the financial world, negative currency amounts are typically formatted that way, so that would be the reason for the parens, and isn't necessarily retarded. I don't know how much C# looks at the localization settings (Regional and Language Settings in the Windows control panel), but it's possible that its default behavior would change based on them.

∴ Nathaniel | 4-Jun-2007 3:13pm est | #10124

Keith (http://keithdevens.com/) wrote:

In the financial world, negative currency amounts are typically formatted that way, so that would be the reason for the parens, and isn't necessarily retarded.

The reason I say C# is retarded here is not because the default is parens -- I figured using parens is standard enough for some group important enough for Microsoft to default to that -- but because it's so complicated to change to a different format.

I don't know how much C# looks at the localization settings (Regional and Language Settings in the Windows control panel), but it's possible that its default behavior would change based on them.

Yes, it does, or at least it's supposed to. My regional settings had negative currency displayed with a negative sign, yet it doesn't respect that. So, I dunno.

Keith | 4-Jun-2007 4:39pm est | http://keithdevens.com/ | #10125

Almutalibi wrote:

That was very good.....I searched alot to find a solution for this problem...

Thanks alot

∴ Almutalibi | 16-Jan-2008 4:14pm est | #10495

R wrote:

Thanks, I was looking for this. But as for it being retarded, it's a sign of the times. If you want a globalized language, that's what you gotta do. Were you expecting if(AmericaFYah), UseNegativeSign = 1? Sure negative signs are fairly standard throughout the world, but you could easily extend that to some other notation that someone else would find odd. Maybe periods instead of commas, for instance. Soon there'd be who knows how many flags.

∴ R | 28-Mar-2008 11:48am est | #10596

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 6 posts

Recent comments XML

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.312s.

(Used 8 db queries)

mobile phone