question

Voytec avatar image
0 Votes"
Voytec asked Voytec commented

UWP two decimals numbers

I am facing a problem of making a variable with two decimal numbers only.
Is it possible to set NumberBox in XAML only read two decimal characters?
If it is not possible to make it in XAML how to format double variable with two decimal characters in C#.

windows-uwp
· 2
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.

@Voytec-6484 I’m a little unclear about your meaning, could you please explain what “set NumberBox in XAML only read two decimal characters” means?
You mean the NumberBox can enter two decimal variables at a time, such as 2 and 78, right? What is your expected behavior?

0 Votes 0 ·

Hello @AryaDing-MSFT
My NumberBox shows 2,0456 for example and I want it to show 2,05.

0 Votes 0 ·

1 Answer

AryaDing-MSFT avatar image
1 Vote"
AryaDing-MSFT answered Voytec commented

Hi,

Welcome to MicrosoftQ&A!

You mean to implement rounding and keep two decimal places, right? For example, for 134.276, you want to change it to 134.28. If so, you could use String.Format method in the ValueChanged event of NumberBox to do this. As follows:

 private void MyNumberBox_ValueChanged(Microsoft.UI.Xaml.Controls.NumberBox sender, Microsoft.UI.Xaml.Controls.NumberBoxValueChangedEventArgs args)
         {
            var value= MyNumberBox.Value;
             MyNumberBox.Text=String.Format("{0:0.00}",value);
         }




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

Thank you Arya.

0 Votes 0 ·