views:

2007

answers:

6

Hi,

My application is currently displaying negative numbers as -1. The users have changed the requirements (just for a change!) and now we will have to display the numbers as (1). Can I enable that for the whole application say changing the web.config or even the app's CultureInfo ? Is there any side effect of doing that since we have lots of pages that contain number validators ?

Thanks !

A: 

You could always write your own custom ToString() method as an extension method, but like you mention, using CultureInfo is probably better. Take a look here:

http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbernegativepattern.aspx

Esteban Araya
An extension method "overloading" ToString() will never get called - The C# compiler prefers calling actual instance methods over extension methods, and since System.Object defines ToString(), you're pretty much stuck.
Erik Forbes
+1  A: 

I'd use String formatting. Making a change to the application's configuration to satisfy a UI requirement is heavy-handed. SteveX wrote a great blog post about String formatting. It's also compatible with markup (aspx) instead of only relevant in code.

From his post:

String.Format(”{0:$#,##0.00;($#,##0.00);Zero}”, value);

    This will output “$1,240.00″ if passed 1243.50. It will output the 
    same format but in parentheses if the number is negative, and will
    output the string “Zero” if the number is zero.

Which isn't exactly what you want, but it's close.

jcollum
but what about when your application only has screens that deal with calculations and stuff i.e. an accountance application? Would you go changing that in all screens? If the requirements change again?
afgallo
Will this application ever run in different countries? Then you need to worry about localization, which brings a new twist. If you really want have one place in your app that gives you back formatted strings, that's fine, use a method in a static class. But calling that method from markup is tough.
jcollum
Yeah this app will run in different countries .... In country A they may want to use -n but country B the they will want (n) ....
afgallo
A: 

String.Format(”{0:f;(f);0”, -1);

Bob
A: 

Are you displaying your data in Gridview/Datagrids? If so then formatting can be applied per bound-column, something like:

<asp:BoundField DataFormatString="{##;(##)}"/>

This only works with integers however...

Mark Glorie
A: 

I've got the following page bookmarked for doing string formatting: http://idunno.org/archive/2004/14/01/122.aspx

About halfway down, it gives the answer:

String.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);

To answer your other question, I wouldn't modify the app.config to make it global, for reasons given in the other answers.

FryGuy
+1  A: 

For currency it is really easy:

String.Format("{0:C}", value)

This will use the culture info for the system.

For normal numbers being data bound, use Mark Glorie's sample.

MSDN Article

Tom Anderson
More points for you Tom. We've set the correct CultureInfo according to the user's country and used the pattern "{0:C}","{0:n}" to use culture infor for the system. Thanks.
afgallo
glad this helped :)
Tom Anderson