Thousand separators & decimal separator in Xamarin Forms Entry

VPrasanth 51 Reputation points
2021-03-19T10:26:04.93+00:00

I would like to know the best approach of fixing the issue of a thousands & decimal separator on a Entry control in Xamarin Forms.
The Entry control is not supporting, both Thousands and Decimal separator, if Numeric type, for multiple locales, out of the box.
Is there anyway we can implement this need of having the same Entry control to accept,

  • Thousands separator by device locale
  • Decimal separator by device country locale

Any thoughts and pointers to achieve this?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-03-19T13:04:14.303+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can try to use following code

       <Entry TextChanged="Entry_TextChanged"></Entry>  
         
        private void Entry_TextChanged(object sender, TextChangedEventArgs e)  
               {  
                   var textbox = (Entry)sender;  
                   var tempValue = double.Parse(textbox.Text, CultureInfo.InvariantCulture);  
                   var newFormat = tempValue.ToString("N2", CultureInfo.InvariantCulture);  
                   textbox.Text = newFormat;  
                   textbox.CursorPosition = newFormat.Length - 3;  
               }  
    

    Here is running screenshot.

    79619-image.png

    ====================
    Update ===========================

    I use Completed to achieve it.

       <Entry  Keyboard="Numeric" HorizontalOptions="FillAndExpand" Completed="Entry_Completed"></Entry>  
         
          private void Entry_Completed(object sender, EventArgs e)  
               {  
                //   var entry = sender as Entry;  
                   var textbox = (Entry)sender;  
                   var tempValue = double.Parse(textbox.Text, CultureInfo.InvariantCulture);  
                   var newFormat = tempValue.ToString("N2", CultureInfo.InvariantCulture);  
                   textbox.Text = newFormat;  
               }  
    

    If you click the complete button like following screenshot, then number will be changed

    80665-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. VPrasanth 51 Reputation points
    2021-03-23T13:35:27.477+00:00

    With "Complete" event, it makes sense to change the value. upon tapping the Done button on the keyboard.
    But, one of my client is expecting to make this change while the text is being changed. Will propose this 'Done'/'Complete' option though.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.