5

I have a WPF screen and a TextBox field which should display a calculated percentage value (which is a double). The value is already correct i.e. it is 21 for 21% (therefore does not need to be multiplied by 100) but it has decimal places and I need to add the % symbol.

I do not wish to round in the C# code as I lose my precision which is required in other calculations. I simply wish to use StringFormat in the XAML to put a format mask on the number. So 21.333 should be seen on screen as 21%.

This is what works so far to truncate off the decimal places but no matter the configuration I try I cannot seem to add the % symbol as well.

Text="{Binding Brokerage, StringFormat='0,0.'}" 

Your help is appreciated.

3 Answers 3

19

Use the fixed-point numeric format string with zero decimal places, F0:

<TextBlock Text="{Binding Brokerage, StringFormat={}{0:F0}%}"/> 
Sign up to request clarification or add additional context in comments.

Comments

3

{Binding OrderQTY, StringFormat={0:0.##}}

Comments

2

Your string format should be in the form of

 "{Binding Path=Percentage, StringFormat={}{0}%}" 

1 Comment

Alas I have tried this already and it leaves the decimal places. Thanks for answering though @TYY.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.