简体   繁体   中英

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. Unfortunatelly very often values are represented as exponential/scientific: 1E-8. I don't want users to see 1E-8 type values. 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#:

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

How to do exactly same formatting using 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?

<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位小数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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