Thousands separator formatter for an entry in xamarin forms RRS feed

  • Question

  • User390044 posted

    I am working on a page where I have several entry to enter numerical values ??(integers and with decimal point), the requirement is that in a field it can be formatted with decimal point and thousands separator (eg: 1.254.356,42), the following code makes the format with value in a good way but the focus of where it is being written in the field is lost:

    private void TbLenght_TextChanged(object sender, TextChangedEventArgs e) { var textbox = (Entry)sender; var tempValue = double.Parse(textbox.Text, culture); var newFormat = tempValue.ToString("N2", culture); textbox.Text = newFormat; }

    What options do I have so that the user when writing the number does not have the problem I am commenting on, and that when I finish writing the text it is in the correct format?

    Tuesday, November 5, 2019 7:48 PM

All replies

  • User382871 posted

    Use Entry to edit the text , then display the formatted content on a Label. page.xaml <StackLayout> <Label x:Name="label" Text="Entry.Text: "/> <Entry TextChanged="Entry_TextChanged" Keyboard="Numeric"/> </StackLayout> page.xaml.cs ``` private void Entry_TextChanged(object sender, TextChangedEventArgs e) { var textbox = (Entry)sender; if (textbox.Text != null) { var tempValue = double.Parse(textbox.Text, CultureInfo.InvariantCulture); var newFormat = tempValue.ToString("N2", CultureInfo.InvariantCulture);

        label.Text = "Entry.Text: " + newFormat;

    } ```

    Wednesday, November 6, 2019 6:26 AM