Question

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 !

Was it helpful?

Solution

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

OTHER TIPS

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.

Check this.. http://msdn.microsoft.com/en-us/library/91fwbcsb.aspx

Converts the string representation of a number in a specified style to its Decimal equivalent.

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

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.

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

This works.

DataFormatString="{0:c0}"
  • Nagative amounts in paranthesis
  • Thousand separater - comma
  • $ symbol in front

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top