This question already has answers here:
1/252 = 0 in c#?
(3 answers)
Closed last year.
double celsius_0 = (100.5 - 32) * (5 / 9);
double celsius_1 = (100.5- 32) * 5 / 9;
Console.WriteLine(celsius_0);
Console.WriteLine(celsius_1);
output:
0
38,0555555555556
Why do they return differnt values?
I dont understand whats happening
It's because here celsius_0 = (100.5 - 32) * (5 / 9)
you count value in the first bracket then in the second one and then you multiply them.
While in the second line (100.5- 32) * 5 / 9 you count value in bracket then multiply it by 5 and at the end divide it by 9
To sum up in mathematics first you do things in brackets and then multiply/divide and finally add/substract
Related
This question already has answers here:
How do I round down a decimal to two decimal places in .NET?
(5 answers)
Closed 5 months ago.
I have the number 12.1799999 and it should become 12.17
How does it work in c#?
var n = 12.1799999M;
n = Math.Floor(n * 100) / 100;
If you intent to round down, then use Math.Floor(number, amountOfDecimals)
amountOfDecimals should be 2 in your case
This question already has answers here:
What's wrong with this division? [closed]
(11 answers)
Format to two decimal places [duplicate]
(3 answers)
Closed 4 years ago.
int a=870,b0=-20, b1=120, a0=0, a1=3584;
double d=(b0 + (b1 - b0) * ((a - a0) / (a1 - a0)));
Console.Write(d);
It gives a result as -20. I want it precisely upto few decimal places.
whats wrong with c#??
You're using int. If you want double precision, convert them to double before doing division, or just simply define them as double in the first place. It's doing whole numbers division the way you wrote it.
you are doing operations between integers thus the result is an integer. Try:
double a=870,b0=-20, b1=120, a0=0, a1=3584;
double d=(b0 + (b1 - b0) * ((a - a0) / (a1 - a0)));
Console.Write(d);
This question already has answers here:
C# is rounding down divisions by itself
(10 answers)
Closed 6 years ago.
int value = 10 * (50 / 100);
The expected answer is 5, but it is always zero. Could anyone please give me detail explanation why it is?
Thanks much in advance.
Because the result of 50/100 is 0 .
50/100 is equals to int(50/100) which returns 0.
Also, if you want to return 5, use this:
int value = (int)(10 * (50 / 100.0));
The result of (50/100.0) is 0.5.
Because you're doing an integer division: (50 / 100) gives 0
Try this:
int value = (int)(10 * (50 / 100.0));
Or reverse the multiply/division
int value = (10 * 50) / 100;
So it's getting multiplied before the divide
You make operation on int values.
50/100 in int is 0.
This question already has answers here:
How to round a integer to the close hundred?
(10 answers)
Closed 7 years ago.
How can i round an (int) so that a number like (22536) is equal to 22000 or 23000?
I haven't found a specific method in the Math class, Math.Round seems to round double only to the nearest int.
By using modulus:
int x = 1500;
int result = x % 1000 >= 500 ? x + 1000 - x % 1000 : x - x % 1000;
It checks if x has any more than 499 when the thousands are stripped, and then rounds it.
This question already has answers here:
Rounding integers to nearest multiple of 10 [duplicate]
(6 answers)
Closed 9 years ago.
I am building an Application based on a excel sheet, at a certain point the sheet uses
=ROUNDUP(701.25;-1) which is returned as 710... how do we do this in C#
I tried Math.Round that returns 701, If I try to use Math.Round() with -1 then i get this
Rounding digits must be between 0 and 15, inclusive.
Please help me out here.
Try this (you'll need to cast the return value to int if necessary):
public static double RoundUp(double value, int digits)
{
double pow = Math.Pow(10, digits);
return Math.Ceiling(value * pow) / pow;
}
This should give you the functionality defined here:
http://office.microsoft.com/en-gb/excel-help/roundup-HP005209242.aspx
use the below two methods.Which may help you
int RoundUp(int toRound)
{
return (10 - toRound % 10) + toRound;
}
int RoundDown(int toRound)
{
return toRound - toRound % 10;
}
Referenece
Rounding integers to nearest multiple of 10