Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is this normal in .NET 6

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

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading