简体   繁体   English

如何在XAML中格式化double值以在没有科学表示的情况下最后删除零?

[英]How to format double value in XAML to remove zeros at the end, without scientific representation?

I need to show double values like: 0.00008 in ListView. 我需要在ListView中显示双精度值:0.00008。 Unfortunatelly very often values are represented as exponential/scientific: 1E-8. 不幸的是,值通常以指数/科学的形式表示:1E-8。 I don't want users to see 1E-8 type values. 我不希望用户看到1E-8类型值。 I don't know and don't want to know decimal points precision of used doubles. 我不知道也不想知道二手双精度小数点的精度。 I can't round doubles. 我不能双打。 I can solve this using c#: 我可以使用C#解决此问题:

string s = doubleValue.ToString("0.####################");  // gives what I need: 0,00008

How to do exactly same formatting using xaml? 如何使用xaml进行完全相同的格式化?

<ListView.View>
  <GridView AllowsColumnReorder="False">
    <GridView.Columns>
     <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Border>
                <Grid>
                    <TextBlock x:Name="textBlock1" Text="{Binding Path=Profit, StringFormat={{???}}" TextTrimming="CharacterEllipsis"  />
                </Grid>
            </Border>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
  </GridView.Columns>
 </GridView>
</ListView.View>

Or how to do it using c# to assign such formatting to textBlock1 in code behind? 或者如何使用c#在后面的代码中将这种格式分配给textBlock1呢?

<TextBlock x:Name="textBlock1"
            Text="{Binding Path=Profit,
                          StringFormat={}{0:0.####################}}"
            TextTrimming="CharacterEllipsis" />

In many cases the reason that you are getting these figures is because you should actually be using decimals rather than doubles. 在许多情况下,获得这些数字的原因是因为您实际上应该使用小数而不是双精度。 If you do calculations with doubles, you often get tiny decimal errors which can be easily avoided. 如果使用双精度进行计算,通常会得到很小的十进制错误,可以很容易避免。

尝试这个:

Text="{Binding Profit, StringFormat='0.00000'}"

尝试使用StringFormat = F6之类的格式,将您的数字格式化为6位小数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM