Skip to content

C# -> VB: Truncate towards zero when casting a fractional number to an integer - #1329

Merged
GrahamTheCoder merged 1 commit into
icsharpcode:masterfrom
lyn1dev:fix/1021-truncate-fractional-cast
Nov 24, 2026
Merged

GrahamTheCoder merged 1 commit into
icsharpcode:masterfrom
lyn1dev:fix/1021-truncate-fractional-cast

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1021

Problem

C# truncates towards zero when converting float/double/decimal to an integral type, while the VB narrowing conversion emitted instead (CInt, CLng, ...) rounds to the nearest integer. So int bar = (int)foo; with foo = 0.6d was converted to Dim bar As Integer = foo, which prints 1 instead of 0; (int)Math.Ceiling(foo) had the same problem.

The constant case fails one step earlier: const int x = (int)0.6; cannot be converted to a VB Const whose initializer is wrapped in a method call, because VB requires a constant expression.

Solution

  • VisitCastExpression wraps the converted operand in Math.Truncate when the destination type is integral and the source type is Single, Double or Decimal, giving CInt(Math.Truncate(foo)). That is the conversion from the issue's expected output. The cast stays around the call so the expression keeps its converted type where that matters (boxing, Object assignment, overload resolution), and the existing simplifier removes it where the destination already converts, which is how Dim bar As Integer = Math.Truncate(foo) is produced.
  • A cast that the C# compiler folds to a constant is emitted as that constant value, since a method call cannot appear in a VB Const initializer: const int TruncatedConstant = (int)0.6; becomes Const TruncatedConstant As Integer = 0.
  • Math.Truncate is emitted even where the operand is already integral, e.g. (int)Math.Ceiling(d) becomes Math.Truncate(Math.Ceiling(d)), because nothing in the operation tree records that the value is integral. It is a no-op at run time; recognising those operands would be a separate, general piece of analysis rather than a special case for Math.Ceiling.
  • One existing expectation changed: checked((int)Math.Pow(46341, 2)) now keeps a Math.Truncate around the Math.Pow call. The overflow behaviour of the converted code is unchanged.
  • At least one test covering the code changed (two C# -> VB conversion tests pinning the output for the issue's repro, other integral target types, a decimal source, a boxed value and a folded constant; a C# -> VB self-verifying test that runs the C# and the converted VB and compares the results, covering positive, negative and decimal values and a cast inside a method return)

Testing

  • The full suite on Windows, .NET 10: 2080 passed, 0 failed, 5 skipped (the skips are pre-existing [Fact(Skip = ...)] tests).
  • The two new conversion tests fail on the unmodified code, with Dim bar As Integer = foo and Dim baz As Integer = Math.Ceiling(foo) as the actual output.
  • The new self-verifying test fails on the unmodified code at run time: Assert.Equal() failure, Values differ, on Assert.Equal(0, (int)positive) in the converted VB, because rounding gives 1.
  • Coverage was measured with dotnet test --collect:"XPlat Code Coverage" on the full suite: every added or edited line in CodeConverter/VB/NodesVisitor.cs is covered, including both branches of the new condition.

Process

  • Attachment level to the code in this PR? Low. The change is small and its shape is fixed by the issue's expected output; the approach, and where it deliberately differs from that expected output, are described above.
  • LLM primarily used: DeepSeek V4.1 Flash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C# -> VB: Math.Truncate is not added when converting Double(/Decimal) to Integer

2 participants