Float Data Type
-
ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???
Cheers :)
-
ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???
Cheers :)
Try this:
float result = (Total*1.0)/(NumberArray.Length*1.0);
.
.
Console.WriteLine("Average " + result);If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"
There are 10 kinds of people: those who understand binary and those who don't
-
ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???
Cheers :)
I assume total is also an integer. IF so what's happening is that you're doing integer division, getting an integer result and then converting the int into a float. What you need to do is cast one of the ints into a float before doing the division.
float result = (float)Total/NumberArray.Length;
Depending on order of operations you might need a second parenthesis to get the desired result.float result = ((float)Total)/NumberArray.Length;
-- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
-
Try this:
float result = (Total*1.0)/(NumberArray.Length*1.0);
.
.
Console.WriteLine("Average " + result);If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"
There are 10 kinds of people: those who understand binary and those who don't
-
Try this:
float result = (Total*1.0)/(NumberArray.Length*1.0);
.
.
Console.WriteLine("Average " + result);If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"
There are 10 kinds of people: those who understand binary and those who don't
Thanks Andrei How silly of me, i should of thought of type 'casting';) this also works float result = ((float)Total/NumberArray.Length);
Cheers :)
-
I assume total is also an integer. IF so what's happening is that you're doing integer division, getting an integer result and then converting the int into a float. What you need to do is cast one of the ints into a float before doing the division.
float result = (float)Total/NumberArray.Length;
Depending on order of operations you might need a second parenthesis to get the desired result.float result = ((float)Total)/NumberArray.Length;
-- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
Yes thanks Dan ;)
Cheers :)
-
Thanks Andrei How silly of me, i should of thought of type 'casting';) this also works float result = ((float)Total/NumberArray.Length);
Cheers :)
I know that casting also works, it was just another way of doing things ;)
There are 10 kinds of people: those who understand binary and those who don't
-
Yes thanks Dan ;)
Cheers :)
Also, you may want to limit the number of decimal places shown, you can do that like this: float fTest = 123.456789f; string sTest = fTest.ToString("N3"); that would put it at 3 decimal places, N5 would show 5 decimal places and so on. Just thought you might like to know. Incase you didn't already.
My current favourite word is: Waffle Cheese is still good though.
-
ok i know this must be a silly question, but im new to c# and still learning..... I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers) This is what i have; float result = Total/NumberArray.Length; //to store avg value in flaot var . . Console.WriteLine("Average " + result); //to show value on screen Where Total is The sum of the numbers NumberArray.Lenghth is lenghth of array For example (20+20+10)/3 = 16.66666 What i see on the screen is 16 Help would be appreciated???
Cheers :)
Ooh - you're hitting an issue that regularly causes confusion and gets a lot of people hot under the collar about .NET. Basically, the issue is that the Total value is an integer. You need to cast the result to a float so this would become
float result = (float)Total/NumberArray.Length;
Deja View - the feeling that you've seen this post before.