Scientific notation XAML

喜你入骨 提交于 2019-12-02 08:37:04

I believe you should use the G format specifier to get (almost) what you want.

<TextBox Text="{Binding Path=CELULARIDAD_CFU, StringFormat='G'}"/>

Within a certain range (different for different number types, see link) the number will then be displayed in normal notation. For large or small enough values, the values will be displayed without trailing zeroes and 2 exponent digits.

Examples for float:

1340000  => 1340000
13400000 => 1.34e+07
0.00054  => 0.00054
0.000054 => 5.4e-05

I didn't found exactly what I want so I decided to use an IValueConverter. Example:

Public Class scientificNotation6
    Implements IValueConverter

    Const EXP As Double = 1000000
    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Return CDbl(value) / EXP 'whatever you want
    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Return CDbl(value) * EXP 'whatever you want
    End Function
End Class

And then in the XAML page:

<TextBox Text="{Binding Path=CELULARIDAD_CONGELADO, Converter={StaticResource scientificNotation6}"/>

I hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!