Reputation: 745
Here is my example
I want to remove $
but the format still the same? How can I possibly do this?
Here is my code in my datagridview
dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "c2";
expected output
(2,138,870,900.11)
19,921,759.23
..and so on
Upvotes: 0
Views: 178
Reputation: 61982
If you still want parenthesis for negatives, try:
dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "#,0.00;(#,0.00)";
Alternatively:
var provider = (CultureInfo)CultureInfo.CurrentCulture.Clone();
provider.NumberFormat.NumberNegativePattern = 0;
dataGridView1.Columns["Amount"].DefaultCellStyle.FormatProvider = provider;
dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "N";
or:
var provider = (CultureInfo)CultureInfo.CurrentCulture.Clone();
provider.NumberFormat.CurrencySymbol = "";
dataGridView1.Columns["Amount"].DefaultCellStyle.FormatProvider = provider;
dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "C";
but these solutions depend on your current culture info (which might be a good thing, but we don't know what culture you use, so we might not know which properties you need to alter).
Instead of setting the FormatProvider
property on each column, you can also do:
// change 'provider' clone as before
Thread.CurrentThread.CurrentCulture = provider;
but that will affect all parts of your application (for the relevant thread at least).
Upvotes: 4
Reputation: 4149
Try this:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencySymbol = string.Empty;
dataGridView1.Columns["Amount"].DefaultCellStyle.FormatProvider = nfi;
dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "c2";
I tried the following in a Console App, and it seem to be working:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencySymbol = string.Empty;
Console.WriteLine(String.Format(nfi, "{0:C2}", -1234567.89));
Upvotes: 2
Reputation: 202
n2 should give you the same without the $. If you do not want the thousand separator, you can use f2.
Upvotes: 1
Reputation: 460208
Couldn't you simply use N2
instead?
dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "N2";
The Numeric ("N") Format Specifier
Upvotes: 2