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);
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:
How do you round a number to two decimal places in C#?
(15 answers)
How can I round a Decimal value upto a precision so that the total number of digits doesnot cross 15?
(2 answers)
Closed 8 months ago.
How can I limit a float to a defined amount of digits? Let's say I want a precision of 4, then it should be:
14.49193 -> 14.49
1.449193 -> 1.449
0.449193 -> 0.4492
0.000449193 -> 0.0004492
I resolved it with this method, there may be a smoother solution.
public static float LimitDigits(float val,int digits) {
if (val == 0) return 0;
return (float)((val<1.0)?Math.Round(val,digits+(int)Math.Floor(-(Math.Log10(val)))):Math.Round(val, Math.Max(0,digits-(int)Math.Floor((Math.Log10(val)+1)))));
}
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
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I feel it's a bug, is it?
decimal s = 30 / 9;
double w = 20 / 9;
Console.WriteLine(s);
Console.WriteLine(w);
and the result is
Is this normal, yes. Is this a bug, no.
You're doing integer math on 30 / 9 which is 3 and then assigning to a decimal which then does the conversion. Same with the second line.
Try this instead:
decimal s = 30m / 9m;
double w = 20.0 / 9.0;
Console.WriteLine(s);
Console.WriteLine(w);
That gives:
3.3333333333333333333333333333
2.2222222222222223
You need to declare your constants in your expression with the correct type. Suffix an m for decimal. And either provide a decimal place or suffix with a d for double.
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