使用 BoundField.DataFormatString 格式化货币数据,( ) 中不能包含 $ 和负数
我有一个 gridview,正在为会计人员填充数据,他们希望我格式化货币值,以便它们显示时不带 $,用逗号分隔数字,并用 ( ) 包围负数,
例如:
12345.67 = 12,345.67
-12345.67 = (12,345.67)
我在互联网上找到了很多例子,这些例子让我很接近,但否定词周围要么没有 ( ),要么包含 $。
I have a gridview that I am populating with data for the folks in accounting and they want me to format currency values so that they display without $'s, with commas separating digits and with negative numbers surrounded by ( )
e.g.:
12345.67 = 12,345.67
-12345.67 = (12,345.67)
I have found lots of examples around the interwebs that get me close but there is either no ( ) around negatives or there is a $ included.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我想基本上问题是,我将根据上述要求格式化货币值的 String.Format() 调用是什么。
在搞乱了一些自定义格式之后,我想通了!
就像魅力一样。
So I guess basically the question was, what is the String.Format() call that I would make to format a currency value to the aforementioned requirements.
After messing around with some custom formats I figured it out!
Works like a charm.
如果它只是没有 ( ) 的数字,那么它就像 {0:N} 一样简单,但是由于您需要 ( ) 表示负数,因此 BoundField 不是您的选择,除非您想在 sql 级别执行某些操作或在代码隐藏中直接操作数据源在它绑定到字段之前。
您的下一个选择是使用带有标签的 TemplateField 并在代码隐藏的 GV RowDataBound 事件中相应地设置该集。
If it was just numeric without ( ) it would be as simply as {0:N} but since you need ( ) for negative numbers BoundField is not your choice unless you want to do something at sql level or code-behind directly manipulating the datasouce before it is bound to the field.
You next option is to use TemplateField with a Label and set the set accordingly in GV RowDataBound Event in code-behind.