Repository navigation
C# -> VB: Truncate towards zero when casting a fractional number to an integer - #1329
Merged
GrahamTheCoder merged 1 commit intoNov 24, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1021
Problem
C# truncates towards zero when converting
float/double/decimalto an integral type, while the VB narrowing conversion emitted instead (CInt,CLng, ...) rounds to the nearest integer. Soint bar = (int)foo;withfoo = 0.6dwas converted toDim 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 VBConstwhose initializer is wrapped in a method call, because VB requires a constant expression.Solution
VisitCastExpressionwraps the converted operand inMath.Truncatewhen the destination type is integral and the source type isSingle,DoubleorDecimal, givingCInt(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,Objectassignment, overload resolution), and the existing simplifier removes it where the destination already converts, which is howDim bar As Integer = Math.Truncate(foo)is produced.Constinitializer:const int TruncatedConstant = (int)0.6;becomesConst TruncatedConstant As Integer = 0.Math.Truncateis emitted even where the operand is already integral, e.g.(int)Math.Ceiling(d)becomesMath.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 forMath.Ceiling.checked((int)Math.Pow(46341, 2))now keeps aMath.Truncatearound theMath.Powcall. The overflow behaviour of the converted code is unchanged.decimalsource, 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 anddecimalvalues and a cast inside a method return)Testing
[Fact(Skip = ...)]tests).Dim bar As Integer = fooandDim baz As Integer = Math.Ceiling(foo)as the actual output.Assert.Equal()failure,Values differ, onAssert.Equal(0, (int)positive)in the converted VB, because rounding gives 1.dotnet test --collect:"XPlat Code Coverage"on the full suite: every added or edited line inCodeConverter/VB/NodesVisitor.csis covered, including both branches of the new condition.Process