Skip to content

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

Merged
GrahamTheCoder merged 2 commits into
masterfrom
claude/amazing-pascal-pqrh77
Nov 24, 2026
Merged

GrahamTheCoder merged 2 commits into
masterfrom
claude/amazing-pascal-pqrh77

Conversation

@GrahamTheCoder

Copy link
Copy Markdown
Owner

Builds on icsharpcode#1329 (fixes icsharpcode#1021).

Problem

C# truncates float/double/decimal → integral casts. VB's CInt etc. round to the nearest integer instead. For the full description, see icsharpcode#1329.

Review of that PR found a bug in its constant path: a folded cast was emitted as a bare literal, so object o = (long)2.5; became Dim o As Object = 2. That boxes an Integer, not a Long. The same applies wherever the type matters, such as overload resolution.

Solution

  • Contains the original commit from C# -> VB: Truncate towards zero when casting a fractional number to an integer icsharpcode/CodeConverter#1329, plus one follow-up commit.
  • Follow-up: the folded constant now goes through the same predefined cast as the non-constant path (CLng(2)). The simplifier still removes the cast where it is redundant, so Const TruncatedConstant As Integer = 0 is unchanged.
  • The truncation and folding logic is merged into TruncateFractionalSource, which runs once before the destination-type switch.
  • Not addressed (pre-existing, out of scope): nullable source or destination ((int)double?, (int?)d) and enum destinations still round.
  • At least one test covering the code changed (boxedConstant case added to CastFractionalToInteger_TruncatedConstantAndBoxedValueAsync; it failed before the follow-up)

Testing

  • The full suite on Linux, .NET 10: 2080 passed, 0 failed, 5 skipped.

Process

  • Attachment level: low
  • LLM primarily used: Claude

Generated by Claude Code

lyn1dev and others added 2 commits November 23, 2026 17:40
The constant path returned a bare literal, so e.g. `object o = (long)2.5;`
became `Dim o As Object = 2`, boxing an Integer instead of a Long. Route the
folded constant through the same cast as the non-constant path; the simplifier
still drops the cast where it's redundant (e.g. Const initializers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019FEr8GNqY4EeHaJnrFj4NL
@GrahamTheCoder
GrahamTheCoder marked this pull request as ready for review November 24, 2026 00:48
@GrahamTheCoder
GrahamTheCoder merged commit 22d8005 into master Nov 24, 2026
3 checks passed
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

3 participants