Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### C# -> VB

* Don't emit Me on the first parameter of an extension method - [#1118](https://github.com/icsharpcode/CodeConverter/issues/1118)
* Truncate towards zero when converting a fractional number to an integer, instead of inheriting VB's rounding - [#1021](https://github.com/icsharpcode/CodeConverter/issues/1021)

## [10.0.2] - 2026-11-08

Expand Down
19 changes: 19 additions & 0 deletions CodeConverter/VB/NodesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,11 +1439,15 @@ public override VisualBasicSyntaxNode VisitCastExpression(CSSyntax.CastExpressio
var sourceType = _semanticModel.GetTypeInfo(node.Expression).Type;
var destType = _semanticModel.GetTypeInfo(node.Type).Type;
var expr = (ExpressionSyntax)node.Expression.Accept(TriviaConvertingVisitor);
if (destType.IsIntegralType() && sourceType.IsFractionalNumericType()) {
expr = TruncateFractionalSource(node, expr);
}
Func<SyntaxKind, VisualBasicSyntaxNode> toNumber = kind => {
if (sourceType?.SpecialType == SpecialType.System_Char)
return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseName(GetQualifiedName(nameof(Microsoft), nameof(Microsoft.VisualBasic), nameof(Microsoft.VisualBasic.Strings.AscW))), ExpressionSyntaxExtensions.CreateArgList(expr));
return SyntaxFactory.PredefinedCastExpression(SyntaxFactory.Token(kind), expr);
};

switch (destType.SpecialType) {
case SpecialType.System_Object:
return SyntaxFactory.PredefinedCastExpression(SyntaxFactory.Token(SyntaxKind.CObjKeyword), expr);
Expand Down Expand Up @@ -1484,6 +1488,21 @@ public override VisualBasicSyntaxNode VisitCastExpression(CSSyntax.CastExpressio
}
}

/// <remarks>
/// C# truncates towards zero when converting a fractional number to an integer, whereas VB rounds. The caller keeps the
/// cast around the result because it fixes the type of the expression (e.g. when it is boxed); the VB simplifier
/// removes it wherever the target type converts implicitly.
/// A cast the C# compiler folds to a constant becomes that (already truncated) constant, since a method call can't
/// appear in a VB constant expression.
/// </remarks>
private ExpressionSyntax TruncateFractionalSource(CSSyntax.CastExpressionSyntax node, ExpressionSyntax expr)
{
var constantValue = _semanticModel.GetConstantValue(node);
return constantValue.HasValue
? _commonConversions.Literal(constantValue.Value)
: SyntaxFactory.InvocationExpression(SyntaxFactory.ParseName(GetQualifiedName(nameof(System), nameof(Math), nameof(Math.Truncate))), ExpressionSyntaxExtensions.CreateArgList(expr));
}

public override VisualBasicSyntaxNode VisitObjectCreationExpression(CSSyntax.ObjectCreationExpressionSyntax node)
{
return SyntaxFactory.ObjectCreationExpression(
Expand Down
27 changes: 27 additions & 0 deletions Tests/TestData/SelfVerifyingTests/CSToVB/TruncatingCasts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Xunit;

public partial class TruncatingCasts
{
[Fact]
public static void TruncatesTowardsZero()
{
double positive = 0.6d;
double negative = -0.6d;
double aboveOne = 1.9d;
decimal decimalValue = 2.9m;

Assert.Equal(0, (int)positive);
Assert.Equal(0, (int)negative);
Assert.Equal(1, (int)aboveOne);
Assert.Equal(-1, (int)-aboveOne);
Assert.Equal(0, Truncated(negative));

long truncatedDecimal = (long)decimalValue;
Assert.Equal(2L, truncatedDecimal);
}

private static int Truncated(double value)
{
return (int)value;
}
}
2 changes: 1 addition & 1 deletion Tests/VB/SpecialConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal partial class TestClass
}", @"Imports System

Friend Partial Class TestClass
Private GENERIC_READ As Integer = Math.Pow(46341, 2)
Private GENERIC_READ As Integer = Math.Truncate(Math.Pow(46341, 2))
End Class
WARNING: Expression at character 85 contained the checked keyword before conversion");
}
Expand Down
56 changes: 55 additions & 1 deletion Tests/VB/TypeCastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,58 @@ Public Sub TestMethod(o As Object)
End Sub
End Class");
}
}

[Fact]
public async Task CastFractionalToInteger_TruncatesTowardsZeroAsync()
{
await TestConversionCSharpToVisualBasicAsync(
@"using System;

public partial class A
{
public static void Main()
{
double foo = 0.6d;
int bar = (int)foo;
int baz = (int)Math.Ceiling(foo);
Console.WriteLine(bar);
Console.WriteLine(baz);
}
}",
@"Imports System

Public Partial Class A
Public Shared Sub Main()
Dim foo = 0.6R
Dim bar As Integer = Math.Truncate(foo)
Dim baz As Integer = Math.Truncate(Math.Ceiling(foo))
Console.WriteLine(bar)
Console.WriteLine(baz)
End Sub
End Class");
}

[Fact]
public async Task CastFractionalToInteger_TruncatedConstantAndBoxedValueAsync()
{
await TestConversionCSharpToVisualBasicAsync(
@"public class A
{
const int TruncatedConstant = (int)0.6;
void Test(double d, decimal m)
{
object boxed = (int)d;
long fromDecimal = (long)m;
object boxedConstant = (long)2.5;
}
}",
@"Public Class A
Const TruncatedConstant As Integer = 0
Private Sub Test(d As Double, m As Decimal)
Dim boxed As Object = CInt(Math.Truncate(d))
Dim fromDecimal As Long = Math.Truncate(m)
Dim boxedConstant As Object = CLng(2)
End Sub
End Class");
}
}
Loading