Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# and VB.Net give different results for the same equation

Tags:

c#

vb.net

VB.Net code that I need to translate to C#:

Dim s = 27 / 15 Mod 1 //result is 0.8

Same equation in C#

var s = 27 / 15 % 1 //result is 0

Why is there a different? Is Mod different between the two?

EDIT: I am translating code from VB to C#, so I need to get the same result as the VB code in my C# code.

like image 970
RJP Avatar asked May 10 '13 17:05

RJP


Video Answer


1 Answers

The division is different between the 2.

In VB.NET you get a floating point type result.

In C# this is integer division (as both operators are integers).

If you use the integer division operator in VB.NET, you will get the same result:

Dim s = 27 \ 15 Mod 1

To get the VB.NET result in C#, you need to ensure one of the division operators is a floating point type:

var s = 27 / 15.0 % 1;
var s = 27.0 / 15 % 1;
var s = 27.0 / 15.0 % 1;
like image 195
Oded Avatar answered Oct 21 '22 00:10

Oded