Reputation: 13035
I'm a novice at C# and WPF technology. I'm trying to understand how can I apply PC's local setting to display time, date and numbers in the UI. I've tried the following code:
public MainWindow()
{
InitializeComponent();
textBox1.AppendText(DateTime.Now.ToString() + "\n");
int a = 123456789;
string sa = a.ToString();
textBox1.AppendText(sa + "\n");
sa = a.ToString(CultureInfo.CurrentCulture);
textBox1.AppendText(sa + "\n");
}
The output in the text box is as follows:
May 29, 13 105731
123456789
123456789
The date and time is formatted correctly according to the PC setting. But the numbers should be shown as 123,456,789.00
according to PC's regional setting, which is not the case.
So, why the number is not formatted when ToString()
is used?
I'm using .NET 4.0.
Upvotes: 3
Views: 360
Reputation: 32559
You want to use overloaded ToString()
method:
a.ToString("N", CultureInfo.CurrentCulture)
This gives me 123 456 789,00
according to my CurrentCulture
(ru-RU
).
See more examples here: http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.80%29.aspx
Upvotes: 2