ASP.NET - Negative numbers in Parenthesis
Asked Answered
S

8

5

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 !

Swap answered 7/1, 2009 at 1:6 Comment(2)
BTW - that's not quite as random a request as it might seem. In some accounting notations, parens are standard instead of negative signs.Andress
it is almost standard in accounting to show () instead of negative.Riggs
R
6

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

Riggs answered 7/1, 2009 at 2:54 Comment(1)
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.Swap
V
4

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.

Viridi answered 7/1, 2009 at 1:18 Comment(3)
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?Swap
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.Viridi
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) ....Swap
W
1

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.

Ways answered 5/9, 2011 at 7:29 Comment(0)
R
0
String.Format(”{0:f;(f);0”, -1);
Runty answered 7/1, 2009 at 1:21 Comment(0)
B
0

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.

Britnibrito answered 7/1, 2009 at 2:43 Comment(0)
G
0

This works.

DataFormatString="{0:c0}"
  • Nagative amounts in paranthesis
  • Thousand separater - comma
  • $ symbol in front
Genotype answered 4/3, 2011 at 0:24 Comment(0)
B
-1

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

Bladder answered 7/1, 2009 at 1:8 Comment(1)
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.Secondbest
E
-1

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

Encephalo answered 7/1, 2009 at 2:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.