Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML StringFormat syntax for decimal places

Tags:

c#

wpf

xaml

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.

like image 879
LaraCroft Avatar asked Dec 01 '22 02:12

LaraCroft


2 Answers

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

<TextBlock Text="{Binding Brokerage, StringFormat={}{0:F0}%}"/>
like image 171
Clemens Avatar answered Dec 09 '22 17:12

Clemens


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

like image 42
Scott Nimrod Avatar answered Dec 09 '22 17:12

Scott Nimrod