is this normal in .NET 6 [duplicate] - c#

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.

Related

Round double off with 2 digits after comma - C# [duplicate]

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

Multiplying large values results to negative value [duplicate]

This question already has answers here:
Multiplying two positive Int32 returns incorrect, negative answer?
(3 answers)
Closed 3 years ago.
double result = 0.0;
result = 7777 * 7777 * 7777 *7777 ;
Instead of getting the result 3658039542829441, I am getting -1283042943. Can someone suggest how to avoid this? I understand the calculation is still under double type's range.
Each 7777 is an integer literal. So it is performing integer multiplication. You will need to make these double literals using the d suffix. Then it will perform double multiplication.
double result = 0.0;
result = 7777d * 7777d * 7777d * 7777d ;

Calculate Power of Double with Double exponent in C# [duplicate]

This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Closed 3 years ago.
I need to calculate x^y where both x and y are Doubles.
I tried using Math.Pow:
Double result = Math.Pow(24.69, 2/3);
The value of result is 1 where it should be 8.4790 ...
Any idea why?
For the exponent you are passing in 2 ints which is do integer division. So it is doing:
Math.Pow(24.69, 0)
To fix this use doubles like this:
Double result = Math.Pow(24.69, 2.0/3.0);

C# Double precision not showing [duplicate]

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);

C# double not working as expected [duplicate]

This question already has answers here:
C# is rounding down divisions by itself
(10 answers)
Closed 7 years ago.
I understand that a double is a decimal.
In the following program the output is 1 even though I thought it would be 1.05 repeating.
static void Main (string[] args)
{
double d = 19 / 18;
Console.WriteLine(d);
Console.ReadKey();
}
Am I misunderstanding double?
You are misunderstanding integer math.
Integer-19 / Integer-18 results in an Integer with value 1.
(that you assign the value to a double is not relevant. The calculation results in an Integer).
To fix it, use:
double d = 19.0 / 18.0;

Categories

Resources