LaraCroft
LaraCroft

Reputation: 63

XAML StringFormat syntax for decimal places

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.

Upvotes: 5

Views: 39182

Answers (3)

Scott Nimrod
Scott Nimrod

Reputation: 11595

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

Upvotes: 3

Clemens
Clemens

Reputation: 128061

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

<TextBlock Text="{Binding Brokerage, StringFormat={}{0:F0}%}"/>

Upvotes: 19

TYY
TYY

Reputation: 2716

Your string format should be in the form of

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

Upvotes: 2

Related Questions