【问题标题】:How to format double value in XAML to remove zeros at the end, without scientific representation?如何在 XAML 中格式化双精度值以在末尾删除零,而无需科学表示?
【发布时间】:2013-06-06 10:36:59
【问题描述】:

我需要在 ListView 中显示双精度值,例如:0.00008。不幸的是,值通常表示为指数/科学:1E-8。我不希望用户看到 1E-8 类型值。我不知道也不想知道使用过的双精度数的小数点精度。我不能圆双打。我可以使用 c# 解决这个问题:

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

如何使用 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>

或者如何使用c#在代码后面将这种格式分配给textBlock1?

【问题讨论】:

    标签: c# wpf xaml listview string-formatting


    【解决方案1】:
    <TextBlock x:Name="textBlock1"
                Text="{Binding Path=Profit,
                              StringFormat={}{0:0.####################}}"
                TextTrimming="CharacterEllipsis" />
    

    【讨论】:

    • 完美答案。效果很好。谢谢你:)
    【解决方案2】:

    在许多情况下,您获得这些数字的原因是您实际上应该使用小数而不是双精度数。如果您使用双精度数进行计算,您通常会得到可以轻松避免的微小小数错误。

    【讨论】:

      【解决方案3】:

      尝试使用类似 StringFormat=F6 的方法,它将您的数字格式化为小数点后 6 位

      【讨论】:

      • 我不能使用固定的小数精度,因为它是动态变化的。
      【解决方案4】:

      试试这个:

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

      【讨论】:

      • 我不能使用固定的小数精度。
      猜你喜欢
      • 2011-02-26
      • 2012-11-08
      • 2020-12-17
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多