-4

How to put decimal value in textbox? Example, I want to put value 0.20 or 20 but to be read as 20%?

2
  • so you want to format your value to a percent mask? Commented Jan 26, 2016 at 20:08
  • You can specify a StringFormat in your binding, as shown in the linked question. If you need something more advanced such as converting either "0.2" or "20" to a percent, you can use a Converter. For an example of a converter that only converts "20", check here - you should be able to easily modify that to check for something like ".20" too. Commented Jan 26, 2016 at 21:26

2 Answers 2

1

Because you should keep this kind of code out of your business logic, you might want to use a StringFormat combined with a custom value converter. Please refer to this article.

In your case, the TextBox might - without knowing your code - look like this:

<TextBox Text="{Binding PercentValue, StringFormat=P, Converter={StaticResource PercentageConverter}}" /> 

The "P" here wold treat 0.25 as 25%.

Sign up to request clarification or add additional context in comments.

1 Comment

Converter has to be imported as a resource to the XAML. Your answer is incomplete and yet copied without understand of how it works yourself.
0

This should work

 decimal val = 0M; decimal.TryParse(YourTextBox.Text, out val); decimal valAsPercent = val * 100; // Then when you want to stuff the new value back into your textbox YourTextBox.Text = valAsPercent.ToString() + "%"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.