Method keeps returning 0 [duplicate] - c#

This question already has answers here:
Division returns zero
(8 answers)
Closed 8 years ago.
I'm trying to get Celsius to take the Fahrenheit temperature as an argument, then return the temperature in Celsius
class Exercise1
{
static void Main(string[] args)
{
double fahrenheit;
Console.WriteLine("Enter farenheit: ");
fahrenheit = double.Parse(Console.ReadLine());
double cels;
Exercise1 me = new Exercise1();
cels = me.Celsius(fahrenheit);
Console.WriteLine(cels);
}
public double Celsius(double fahrenheit)
{
double celsius;
celsius = 5 / 9 * (fahrenheit - 32);
return celsius;
}

Your problem is integer division in your Celsius function. This line
celsius = 5 / 9 * (fahrenheit - 32);
Will always be 0 because 5 / 9 will be divided as integers which will always give you 0. To force floating-point division, one if your integers must be a double. So do this:
celsius = 5.0 / 9 * (fahrenheit - 32);
Note the 5.0 will force floating-point division.

5 / 9 would be seen as integers and thus make the entire calculation integer maths in this case.
Meaning you are essentially getting 0 * (fahrenheit - 32).
Cast either or both 5 and 9 to a double to force floating point maths.

Related

Percentage calculation always returns me 0 [duplicate]

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 4 years ago.
I'm calculating the percentage to add in a datagrid, but when I report the data it always returns me 0, what am I doing wrong?
if I have the following case: the variable quantidadeInstalada has the value of 10 and the goal has 20 the concluido would have to return me 50 but it returns me 0
private void AdicionarPessoa()
{
string Valida = ValidaPessoa();
if (Valida.Equals(""))
{
double concluido=0, falta=0;
int quantidadeInstalada = Convert.ToInt32(ttbQuantidade.Text);
int meta = Convert.ToInt32(ttbMetaPessoa.Text);
concluido = (quantidadeInstalada/meta)*100;
falta = 100-concluido;
MessageBox.Show(concluido.Text);
}
else
MessageBox.Show(Valida);
}
It's probably due to the precision of the int. Use a decimal or double instead.
When we use an integer, we lose precision.
Console.WriteLine(100 / 17); // 5
Console.WriteLine(100 / 17m); // 5.8823529411764705882352941176
Console.WriteLine(100 / 17d); // 5.88235294117647
Console.WriteLine(100 / 17f); // 5.882353
Since integers always round down, 0.99 as an integer is 0.
Note that for precision, the types of the inputs matters.
double output = input1 * input2;
For example:
double outputA = 9 / 10;
Console.WriteLine(outputA); // 0
double outputB = 9 / 10d;
Console.WriteLine(outputB); // 0.9
double outputC = 9d / 10;
Console.WriteLine(outputC); // 0.9
Here is a Fiddle.
It's because you're doing integer division. You can fix it by casting one of the variables to a double like this:
concluido = ((double)quantidadeInstalada/meta)*100;
When you're dividing int by int (as in "quantidadeInstalada/meta"), you'll get an int. Either use a fractional type (e.g. decimal, double) from the very beginning, as Shaun suggested, or (if the integral types have to stay), cast the values to a fractional type in the division expression (as itsme86 shown).

multiplying a decimal by a floating point number [duplicate]

This question already has answers here:
Division returns zero
(8 answers)
Closed 5 years ago.
Im trying to get 75% of the CTotal but as it is a decimal number it is rounding 0.75 to 0, does anyone know of a work around
decimal refundtot = order.CTotal;
//change it as it is making it = 0
refundtot = (75 / 100) * refundtot;
refund.RefundTotal = refundtot;
You should use one decimal number when you dividing numbers.
Your code should look like this :
efundtot = ((decimal)75 / 100) * refundtot;

C# not calculating a decimal result properly [duplicate]

This question already has answers here:
Extremely basic division equation not working in c#
(3 answers)
Closed 9 years ago.
(5 / 15) * 1185 should give 395.
decimal Test = (5 / 15) * 1185;
This, however, returns 0. What am I doing wrong?
5 / 15 is an integer division and returns 0. Use 5m / 15m to force decimal.
m is the C# suffix to signify to the compiler that the number your wrote is a decimal, even if it looks like an int. You can use f for floats and d for doubles (or 5.0).
While on the topic of suffixes, there is also L for long but that wouldn't have helped your with your division because it is also an integer type.
5 // Int32
5L // Int64
5d // Double
5.0 // Double
5m // Decimal
5f // Single
You need to use floating point division, not integer division. You can get the correct result by making it a floating point number, or using m (to specify it as a decimal) behind the 15:
decimal Test = (5 / 15.0) * 1185;
or
decimal Test = (5 / 15m) * 1185;
You could also use a lambda expression :
Func<decimal, decimal, decimal, decimal> exp = (x, y, z) => (x / y) * z;
Console.WriteLine(exp(5, 15, 1185));
outputs:
394.999999
as you are expecting 395 you will need to use a Math.Round
Console.WriteLine(Math.Round(exp(5, 15, 1185), 1));
outputs: 395.0
5/15 is 0 because you are using integer division.
You can try use one of the following code:
decimal test = (5m / 15) * 1185
decimal test = ((decimal)5 / 15) * 1185
decimal test = (5.0 / 15) * 1185
The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal type does not eliminate the need for rounding but rather minimizes errors due to rounding.
Link: http://msdn.microsoft.com/en-us/library/364x0z75

C# generating random double number range [duplicate]

This question already has answers here:
Random Number Between 2 Double Numbers
(13 answers)
Closed 9 years ago.
Hi how can i generate a range of "double" numbers? For example how can i generate numbers between 2.50 and 7.20
There is a method in Random() class for "int" numbers Next(Int32, Int32) is there something similar for double?
You can write an extension method to Random for Random.NextDouble(double MinValue,double MaxValue) so that you can use it everywhere:
public static class RandomExtensions
{
public static double NextDouble(this Random RandGenerator, double MinValue, double MaxValue)
{
return RandGenerator.NextDouble() * (MaxValue - MinValue) + MinValue;
}
}
Get a value from 0 to 1, then multiply it by 7.20 - 2.50 and add 2.50.
double result = (random.NextDouble() * (7.2 - 2.5)) + 2.5;
Yes, it's called Random.NextDouble(). This returns a double between 0 and 1.
var value = lower + (random.NextDouble() * (upper - lower))
will return what you need.

How to round decimal to an int in c# [duplicate]

This question already has answers here:
How might I convert a double to the nearest integer value?
(8 answers)
Closed 9 years ago.
I'am producing decimal such as:
0.8235294117647058823529411765
0.1764705882352941176470588235
I'd like to multiply them by 10 then round them. If the second number after the dot is less than 5 then make it 0. Otherwise, make it 1. For the above examples, that would be:
8
2
The result should be put in a int.
I thought it is simple.
Math.Round(10 * your_decimal);
Reference: http://msdn.microsoft.com/en-us/library/3s2d3xkk.aspx
You can use Math.Round() if you want to keep it as a decimal or round to a certain precision.
decimal dec = 0.8235294117647058823529411765m;
decimal rounded = Math.Round(dec * 10); // 8m
decimal roundedToOne = Math.Round(dec * 10, 1); // 8.2m
FYI, an explicit conversion is defined for decimal to int so you can round down by casting to an int
int a = (int)(dec * 10); // 8
This could be combined with a condition to round the number up and down
decimal num = dec * 10;
int a = (int)num + (num % 0 < .5m ? 0 : 1); // 8

Categories