简体   繁体   中英

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM