Search code examples
xamlsilverlight-5.0scientific-notation

Scientific notation XAML


I'm using scientific notation in XAML. I do:

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

The problem is that for 1710000 is showing: 1.710000e+006, which is correct but I would like to see 1.71e+6. How is this specified in XAML? (Not in code)


Solution

  • 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.