如何在 XAML、WPF 中四舍五入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22862382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to round double in XAML, WPF?
提问by Diemauerdk
I have the following code in XAML:
我在 XAML 中有以下代码:
<TextBlock Text="{Binding XValue}" />
The XValue contains a lot of decimals(1952.230822830529)
XValue 包含很多小数(1952.230822830529)
I want it to contain only 2 decimals like 1952.23 and i want to do this in the XAML code.
我希望它只包含 2 个小数,如 1952.23,我想在 XAML 代码中执行此操作。
I have searched the internet but not found a solution to my problem - so is it even to do this in XAML using string format?
我已经在互联网上搜索过但没有找到解决我的问题的方法 - 那么是否可以使用字符串格式在 XAML 中执行此操作?
回答by alan
This is a duplicate of this post (also the top result on google): How to format number of decimal places in wpf using style/template?
这是这篇文章的副本(也是谷歌上的最高结果):如何使用样式/模板格式化 wpf 中的小数位数?
Try one of these:
尝试以下方法之一:
<TextBox Text="{Binding XValue, StringFormat=N2}" />
<TextBox Text="{Binding XValue, StringFormat=#,#.00}" />
回答by Bolu
If XValueis number (not stringtype) you could use
如果XValue是数字(不是string类型),您可以使用
<TextBlock Text="{Binding XValue, StringFormat={}{0:N2}}"/>

