question

VaranasiPrasanth-3918 avatar image
0 Votes"
VaranasiPrasanth-3918 asked VaranasiPrasanth-3918 answered

Thousand separators & decimal separator in Xamarin Forms Entry

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?



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.



image.png (31.0 KiB)
image.png (44.4 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I had already implemented this logic and implemented. The drawback with this implementation is that the cursor is not allowing to enter the decimal values.

0 Votes 0 ·
LeonLu-MSFT avatar image LeonLu-MSFT VaranasiPrasanth-3918 ·

the cursor is not allowing to enter the decimal values.

Do you mean that tenths, hundredths value?

0 Votes 0 ·

Hi,
I mean to ask about the decimal fractions. Using the above given logic, the user will not be able to enter the decimal value, as the curser always goes before the decimal(.).
This logic may be suitable for the whole numbers but not for double/decimal data types.

0 Votes 0 ·
Show more comments
VaranasiPrasanth-3918 avatar image
0 Votes"
VaranasiPrasanth-3918 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.