StringFormat double number on Binding

In the latest release of Silverlight and WPF, Microsoft has introduced StringFormat on Binding value.
The StringFormat permit to avoit to write many converter to show the right data value.

Microsoft on his pages show only this samples:

String

For a string with the value “WinPhone rocks”.

StringFormat=\{0,20\} : “      WinPhone rocks”
StringFormat=\{0,-20\} : “WinPhone rocks      ”
StringFormat=’I just typed "\{0\}".’ : “I just typed “WinPhone rocks”.”

Numbers

For a double with the value : 18749.3574

StringFormat=\{0:C\} : $18,749.36

StringFormat=\{0:F\} : 18749.36

StringFormat=\{0:N\} : 18,749.36

StringFormat=\{0:E\} : 1.874936E+004

StringFormat=\{0:P\} : 1,874,936.74%

You can also use the format:

StringFormat=\{0:[letter][number]\} :

so that [letter] indicates why kind of formatting and [number] indicates the decimal place:

StringFormat=\{0:E5\} : 1.87494E+004

StringFormat=\{0:F3\} : 18,749.357

StringFormat=\{0:P0\} : 1,874,936%

StringFormat=’Amount available: \{0:C0\}” : Amount available: $18,749

Dates

Date formatting is extremely flexible with a large number of preset options and a custom formatting structure that gives a lot of options. If you implement the localization feature above, it is like its own special kind of magic.

For the date (with US localization): 1/25/2013 3:30:23 PM

StringFormat=\{0:d\} : 1/25/2013

StringFormat=\{0:D\} : Thursday, January 25, 2013

StringFormat=\{0:f\} : Thursday, January 25, 2013 3:30 PM

StringFormat=\{0:F\} : Thursday, January 25, 2013 3:30:23 PM

StringFormat=\{0:g\} : 1/25/2013 3:30 PM

StringFormat=\{0:G\} : 1/25/2013 3:30:23 PM

StringFormat=\{0:M\} : January 25

StringFormat=\{0:R\} : Thu, 25 Jan 2013 15:30:23 GMT

And if none of these formats suit you, you can define your own DateTime format:

StringFormat=’hh:mm:ss:fff tt’ : 03:30:23.423 PM

StringFormat=’MM/dd/yy’ : 01/25/13

StringFormat=’MMMM dd, yyy g’ : January 25, 2013 A.D.

StringFormat=’ddd, MMM dd, yyyy’ : Thu, Jan 25, 2013


But how can I format a double value with 3 decimals?
The first solution is:

StringFormat=\{0:n3\}

in this solution set 3 the decimal to all number like this:

17.100

If I want show the digit only if different from 0 usualy we do:
myDoubleValue.ToString(“0.###”)

the right string format is:
StringFormat=\{0:0.###\}

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in Silverlight, WP7, WP8, WPF, xaml e contrassegnata con , , , , , , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...