Floating Value by Dividing in a DataGridView !
-
Hi All ! in a DataGridView , for example :
DataGridView1[0,0].Value=1.2*3;
Result is : 3.6 But if :
DataGridView1[0,0].Value=2/3
Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks
-
Hi All ! in a DataGridView , for example :
DataGridView1[0,0].Value=1.2*3;
Result is : 3.6 But if :
DataGridView1[0,0].Value=2/3
Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks
The result is 0 because you are dividing an integer by an integer, which leads the compiler to treat the result as an integer. The trick is to cast the first number to a double and then base the calculation on this.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi All ! in a DataGridView , for example :
DataGridView1[0,0].Value=1.2*3;
Result is : 3.6 But if :
DataGridView1[0,0].Value=2/3
Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks
by writing it 2/3 the compiler thinks you are doing integer division. You should explicitly denote one of the inputs as a float by using the f symbol:-
DataGridView1[0,0].Value=2f/3;
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
The result is 0 because you are dividing an integer by an integer, which leads the compiler to treat the result as an integer. The trick is to cast the first number to a double and then base the calculation on this.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Yes , It's a Big Mistake for Me ! Thanks !
-
by writing it 2/3 the compiler thinks you are doing integer division. You should explicitly denote one of the inputs as a float by using the f symbol:-
DataGridView1[0,0].Value=2f/3;
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
:doh: Thanks!
-
by writing it 2/3 the compiler thinks you are doing integer division. You should explicitly denote one of the inputs as a float by using the f symbol:-
DataGridView1[0,0].Value=2f/3;
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
Wayne Gaylard wrote:
by writing it 2/3 the compiler thinks you are doing integer division
FTFY :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Wayne Gaylard wrote:
by writing it 2/3 the compiler thinks you are doing integer division
FTFY :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
I was giving him(OP) the benefit of the doubt :)
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
Hi All ! in a DataGridView , for example :
DataGridView1[0,0].Value=1.2*3;
Result is : 3.6 But if :
DataGridView1[0,0].Value=2/3
Result is : 0 plz help , how can I show a floating value by Dividing Two Number in a cell of DataGridView Thanks
That is Modular division. if it is like 4/3 ans will be 1. U can get the ans if u make it like 2.0/3 or 2/3.0