StringFormat with value zero

落爺英雄遲暮 提交于 2019-12-13 21:26:35

问题


I'm developing a wpf application with componentart I have a textblock like this

<TextBlock FontSize="28" Text="{Binding DataPoint.Y, StringFormat=\{0:#\\%\}}" Foreground="Black">

As you can see, my StringFormat puts a '%' sign after the number, but if my data is 0.0 (I fill the component in code behind, my variable is a double) I get "%", but I want to get "0%", how can I do this?


回答1:


you can use different StringFormat. Instead of 0:# put 0:0:

<TextBlock FontSize="28" Text="{Binding Path=MyDouble, StringFormat=\{0:0\\%\}}" Foreground="Black"/>

quoting MSDN:

#: Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

0: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.



来源:https://stackoverflow.com/questions/17594523/stringformat-with-value-zero

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