Format number with + and - sign

浪子不回头ぞ 提交于 2019-11-30 05:24:58

问题


I have some WPF textblocks in a stackpanel that I want to databind and format.

E.g. the following formats a date 24h style without the seconds part:

<TextBlock Text="{Binding MyCustomObject, StringFormat={}{0:HH:mm}}" />

Now, I would like to bind an integer and also display the + and - sign (i.e. +6 or -4).

<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#}}" />

This however, does not work. Is this possible or do I have to write a complete converter just for this?

EDIT

Nikolays post led me to the answer:

<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" />

In essence you provide a format for positive numbers, negative numbers and an optional part what to do with zero. In this case I stated that a zero should be displayed as an empty string.

Regards,

Michel


回答1:


Try this:

<TextBlock Text="{Binding MyOtherCustomObject, StringFormat={}{0:+#;-#;''}}" />

This article has nice samples of int formatting - http://www.csharp-examples.net/string-format-int/



来源:https://stackoverflow.com/questions/9782131/format-number-with-and-sign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!