Skip to content

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

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

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

Conversation

@GrahamTheCoder

Copy link
Copy Markdown
Member

Builds on #1329 (fixes #1021).

Problem

C# truncates float/double/decimal → integral casts. VB's CInt etc. round to the nearest integer instead. For the full description, see #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 #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 changed the title Claude/amazing pascal pqrh77 C# -> VB: Truncate towards zero when casting a fractional number to an integer Nov 24, 2026
@GrahamTheCoder
GrahamTheCoder merged commit 5ed6f9e into icsharpcode: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