0

anyone knows how to add two float numbers with currency sign and place the sum into a dataGridview cell?

this is my code:

private void button8_Click(object sender, EventArgs e)
{
    double a =0.70;
    double b = 0.50;
    dataGridView2.Rows[0].Cells[1].Value = "£" + (a+b);
}

if I declare a and b as float it gives error. if I declare a and b as Double, the result is £1.2 as shown in photo but NOT £1.20 . I made column 1 properties to contain a currency value ( result = £1.2) I made column 1 properties to contain a numeric value and the result is same.

how can I make it £1.20? is there any way to do it?

thank you

dataGridView

Dmitry
  • 13,797
  • 6
  • 32
  • 48
naouf
  • 627
  • 2
  • 18
  • 28
  • What happen if your remove the string concatenation with your currency symbol when you set the result of the sum? (Of course still having the column set to Currency with two decimals) – Steve Oct 24 '15 at 21:28
  • 1
    The issue with declaring as float is that real literals without a suffix are automatically doubles. Use the 'f' suffix for floats. That said, you should be using `decimal` to represent currency values, not `double` or `float`. Use the 'm' suffix for decimals. – Mike Zboray Oct 24 '15 at 21:37
  • Hi Steve. same result. – naouf Oct 24 '15 at 22:08

6 Answers6

3

The double type doesn't keep track of how many decimal places were there when the result was computed. As far as it is concerned, 1.2 and 1.20 and 1.200 are all the same value.

You want a specific string representation of that number, with exactly two decimal places. The easiest way to get the desired string representation is using a format string. For example, you can do this:

(a + b).ToString("0:00");

In cases where you want to format multiple expressions, you may find this alternative more convenient:

string.Format("{0:0.00} {1:0.00} {2:0.0}", a, b, a + b)

Keep in mind that float and double are tricky. You probably want to handle money using the decimal type instead.

Community
  • 1
  • 1
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
1

You can use:

dataGridView2.Rows[0].Cells[1].Value = (a + b).ToString("'£'0.00"));
Gustav
  • 53,498
  • 7
  • 29
  • 55
1

To do this, you need to set a format for the string. Try using this:

dataGridView2.Rows[0].Cells[1].Value = string.Format("£{0:0.00}", a + b)
lub094
  • 103
  • 7
0

You can try this:

dataGridView2.Rows[0].Cells[1].Value = "£" + (a+b).ToString("0.00");
Fran
  • 1
  • 1
0

Thank you all for your help. now it is solved. I played around with cell format type and suddenly it changed to £1.20.

naouf
  • 627
  • 2
  • 18
  • 28
0

Definitely keep in mind the explanations given by Theodoros, especially the suggestion to use the decimal type.

That said, an alternative is to use a System.Globalization.CultureInfo. This has the advantage of correctly handling the symbol, decimal digits, decimal separator, group separator, and group sizes. This way you see results such as £1,234,567.89 instead of £1234567.89.

CultureInfo gb = new CultureInfo("en-GB");
string result = (a + b).ToString("c", gb.NumberFormat);

A further advantage is that parsing out the original value is just as easy:

decimal amount = Decimal.Parse(result, NumberStyles.Currency, gb);
OhBeWise
  • 5,350
  • 3
  • 32
  • 60