Saturday 7 March 2009

Commar format in WPF

Add the following reference
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Then add the following static resource
UserControl.Resources
sys:String x:Key="CommaFormat" {0:#,0} /sys:String
...
Then within the binding element of the Text use String Format
TextBlock Foreground="Black" Text="{Binding COVER, StringFormat={StaticResource CommaFormat}}" HorizontalAlignment="Right"/
There is no way to directly add the string format into the Text property
The alternative using PropertyConverters requires a new class and therefore much more code than this...
8<---
Notes from
http://www.sloppycode.net/articles/formatstrings.aspx
Console.WriteLine("{0:g}", DateTimeKind.Utc); // general
Console.WriteLine("{0:f}", DateTimeKind.Utc); // string
Console.WriteLine("{0:d}", DateTimeKind.Utc); // integer
Console.WriteLine("{0:x}", DateTimeKind.Utc); // hex

Console.WriteLine("{0} {1} {2}",100,200,3000);
Output:
100 200 3000
Console.WriteLine("{0:X} {1:N1} {2:#,#}",100,200,3000);
Output:
64 200.0 3,000
Console.WriteLine("(amount):{0,30:C}", 1123.27);
Output:
(amount): £1,123.27
Console.WriteLine("{0,-30:C}(amount)", 1123.27);
Output
£1,123.27 (amount)
double neg = -1000.123;
double pos = 1000.123;
double z = 0;
Console.WriteLine("{0:A)0.00;B)#.##}", pos);
Console.WriteLine("{0:A)0.00;B)#.##}", z);
Console.WriteLine("{0:A)0.00#;B)#.##}", neg);
Console.ReadLine();
Output
A)1000.12
A)0.00
B)1000.12
double x = 67867987.88666;
Console.WriteLine("{0:C}",x);
Console.WriteLine("£{0:N}", x);
Console.WriteLine("£{0:#,#.###}", x);
Output:
£67,867,987.89
£67,867,987.89
£67,867,987.887
// No number specifier
Console.WriteLine("a) {0}", numSingle);
// Round up, ignore any decimal points.
Console.WriteLine("a) {0:##}", numSingle);
// Round up to 1 dp.
Console.WriteLine("b) {0:.#}", numSingle);
// Round up to 2 dp.
Console.WriteLine("c) {0:.##}", numSingle);
// Round up to 3 dp.
Console.WriteLine("d) {0:.###}", numSingle);
// Floats work up to 7 digits, then any extra digit after the decimal place is rounded up, or ignored.
// So 4 decimal places is the maximum a number up to 1000 can store, e.g. 999.1234.
// It's recommended to always use double (Double) or decimal (Decimal) for applications involving currencies.
double numDouble = 1000000.5664;
float numSingle2 = 999.9999f;
Console.WriteLine("e) {0:.####}", numDouble);
Console.WriteLine("e2) {0:.####}", numSingle2);
Console.WriteLine("e3) {0:##}", numSingle2);
Output:
a) 10.566
a) 11
b) 10.6
c) 10.57
d) 10.566
e) 1000000.5664
e2) 999.9999
e3) 1000
//
// In built formatters. These can have digit specifiers after them, such as {0:D5}
//
Console.WriteLine("");
Console.WriteLine("a) [C]urrency - {0:C}", 1123.27); // Very similar to N (but no rounding), but with the currency symbol. See the section on regions.
Console.WriteLine("b) [D]ecimal - {0:D5}", 1123); // Integers only. The '5' sets the number of zeros to pad to the left.
Console.WriteLine("c) [F]ixed - {0:F3}", 1123.275); // Fixed decimal places including rounding.
Console.WriteLine("d) [G]eneral - {0:G}", 1123.275); // Scientific or decimal notation, whichever is shorter. Has a long set of rules in MSDN.
Console.WriteLine("e) [N]umber - {0:N}", 1123.275); // Similar to fixed, but includes ',' thousand seperator and is rounded to 2 dp by default.
Console.WriteLine("f) [P]ercent - {0:P}", 0.2345); // Percentage to 2 dp. So 0.99998 is 100%
Console.WriteLine("g) [P]ercent - {0:#%} ", 0.915); // This shows the percentage but as a whole number, rounded
double d1 = 1234.9998;
string rd = string.Format("{0:R}", d1);
double d2 = Double.Parse(rd);
Debug.Assert(d1 == d2);
Console.WriteLine("h) [R]ound trip - {0:R}", 0.9998); // Allows you to parse the resulting string using Single.Parse() or Double.Parse()
Console.WriteLine("i) he[X]adecimal - 0x{0:X}", 255); // Integers only
Console.WriteLine("j) [E]xpontial - {0:E}", 10000000);
Output:
a) [C]urrency - £1,123.27
b) [D]ecimal - 01123
c) [F]ixed - 1123.275
d) [G]eneral - 1123.275
e) [N]umber - 1,123.28
f) [P]ercent - 23.45 %
g) [P]ercent - 92%
h) [R]ound trip - 0.9998
i) he[X]adecimal - 0xFF
j) [E]xpontial - 1.000000E+007
// --- Default formats ---
// Tokens:
// dD tT fF gG rsu M Y
// d = short/long [d]ate
// t = short/long [t]ime
// f = short/long [f]ull
// g = short/long [g]eneral or default
// rsu = RFC or ISO format, last 2 are sortable
// M = day & month
// Y = month & year
// --- Custom formats ---
// dd ddd dddd
// 15 Mon Monday
// MM MMM MMMM
// 06 Jun June
// yy yyyy
// 08 2008
// hh HH
// 01 13
// mm
// ss
// tt
// AM or PM

public class FormatProvider : IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType is IFormatProvider)
return this;
else
return null;
}
}
public string ToString(string format, IFormatProvider formatProvider)
{
// handle format
}
public class User : IFormattable
{
private int _age;
private string _name;
///
/// Gets/sets the Name.
///

public string Name
{
get { return _name; }
set { _name = value; }
}
///
/// Gets/sets the Age.
///

public int Age
{
get { return _age; }
set { _age = value; }
}
///
/// Object.ToString() implementation
///

public override string ToString()
{
return string.Format("'{0}', age {1}", Name, Age);
}
///
/// Just a format string, functionality you find in DateTime.Now.ToString()
///

public string ToString(string format)
{
return ToString(format, null);
}
///
/// IFormattable.ToString() implementation
///

public string ToString(string format, IFormatProvider formatProvider)
{
// Default format
if ( string.IsNullOrEmpty(format) format == "G" )
return string.Format("'{0}', age {1}", Name, Age);
if (format == "name")
return Name;
else if (format == "age")
return Age.ToString();
else
{
// The IFormattable example shows branching based on the format. This is fine if the class
// is being used with composite formatting, however to support the DateTime.Now.ToString("xxx")
// style formatting, we need the below.
string result = format;
result = result.Replace("name", Name);
result = result.Replace("age", Age.ToString());
return result;
}
}
}
static void Main(string[] args)
{
User user = new User() { Name = "Chris", Age = 30 };
Console.WriteLine("Default format: {0}", user.ToString());
Console.WriteLine("Composite format: {0:name} is {0:age}",user);
Console.WriteLine("ToString(format): {0}", user.ToString("This user (name) is age"));
Console.ReadLine();
}
Output:
Default format: 'Chris', age 30
Composite format: Chris is 30
ToString(format): This user (Chris) is 30