簡體   English   中英

如何在XAML中格式化double值以在沒有科學表示的情況下最后刪除零?

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

我需要在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呢?

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

在許多情況下,獲得這些數字的原因是因為您實際上應該使用小數而不是雙精度。 如果使用雙精度進行計算,通常會得到很小的十進制錯誤,可以很容易避免。

嘗試這個:

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

嘗試使用StringFormat = F6之類的格式,將您的數字格式化為6位小數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM