Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: VB float division (/) is converted as an integer division #1160

Closed
jrfstr opened this issue Jan 4, 2025 · 6 comments · Fixed by #1161
Closed

VB -> C#: VB float division (/) is converted as an integer division #1160

jrfstr opened this issue Jan 4, 2025 · 6 comments · Fixed by #1161
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@jrfstr
Copy link

jrfstr commented Jan 4, 2025

VB.Net input code

Dim TotalRead as UInteger
Dim ContentLenght as UInteger  (It is supposed that TotalRead < ContentLength)
Dim porcentage As Integer = Convert.ToInt32((TotalRead / ContentLenght) * 100.0)

In VB TotalRead / ContentLenght is a float number. I guess the operands are converted to float before perform the division. Percentage can take all values between 0 and 100.

Erroneous output

uint TotalRead;
uint ContentLenght;
int porcentage = Convert.ToInt32(TotalRead / ContentLenght * 100.0d); 

Here TotalRead / ContentLenght is an integer and porcentaje only takes two values: 0 and 100

Expected output

int porcentage = Convert.ToInt32((double)TotalRead / (double)ContentLenght * 100.0d);

Details

  • Product in use: Code Converter (VB - C#) (VS extension)
  • Version in use: 9.2.6.0
@jrfstr jrfstr added the VB -> C# Specific to VB -> C# conversion label Jan 4, 2025
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Jan 4, 2025

Sorry I can't reproduce the issue
image

This might be some difficulty with the dot net version for example. What project type?
I assume you can't reproduce the issue here: https://icsharpcode.github.io/CodeConverter/

@jrfstr
Copy link
Author

jrfstr commented Jan 4, 2025

The dot net version is .NET 9.0. The project type is a class library (in VB).

In https://icsharpcode.github.io/CodeConverter/ I can't reproduce the issue. Things gone right there.

@jrfstr
Copy link
Author

jrfstr commented Jan 4, 2025

Sorry, that was not the code. Try this in the link:

Public Class VisualBasicClass
public sub x()
Dim TotalRead as long
Dim ContentLenght as long?  '(It is supposed that TotalRead < ContentLength)
Dim porcentage As Integer = Convert.ToInt32((TotalRead / ContentLenght) * 100.0)
End Sub
End Class

The output in c# is

using System;

public partial class VisualBasicClass
{
    public void x()
    {
        var TotalRead = default(long);
        var ContentLenght = default(long?);  // (It is supposed that TotalRead < ContentLength)
        int porcentage = Convert.ToInt32(TotalRead / ContentLenght * 100.0d);
    }
}

@GrahamTheCoder
Copy link
Member

Yep that repros. It's an issue specifically with the logic around nullable not working correctly with logic around casting

GrahamTheCoder added a commit that referenced this issue Jan 5, 2025
…nversion - fixes #1160

Changes the results of a few other tests so likely needs refinement
@GrahamTheCoder
Copy link
Member

I made a start on fixing up the issue, but there are a couple of situations in other tests that need thinking about before it could be merged

@jrfstr
Copy link
Author

jrfstr commented Jan 5, 2025

Thanks very much for your this project!

GrahamTheCoder added a commit that referenced this issue Jan 5, 2025
Consider the underlying type if the conversion is a nullable value conversion - fixes #1160
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
2 participants