質問

Here is my example

enter image description here

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
役に立ちましたか?

解決

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).

他のヒント

Couldn't you simply use N2 instead?

dataGridView1.Columns["Amount"].DefaultCellStyle.Format = "N2";

The Numeric ("N") Format Specifier

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));

n2 should give you the same without the $. If you do not want the thousand separator, you can use f2.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top