Skip to content

VB -> C#: Optimize Double(/Decimal)-to-Integer conversion #1020

@tats-u

Description

@tats-u

VB.Net input code

Imports System

Public Class A
    Public Shared Sub Main()
        Dim foo = 0.6
        Dim bar = CInt(Math.Truncate(foo))
        Dim baz = CInt(Math.Ceiling(foo))
        Console.WriteLine(bar)
        Console.WriteLine(baz)
    End Sub
End Class

Erroneous output

using System;

public partial class A
{
    public static void Main()
    {
        double foo = 0.6d;
        int bar = (int)Math.Round(Math.Truncate(foo));
        int baz = (int)Math.Round(Math.Ceiling(foo));
        Console.WriteLine(bar);
        Console.WriteLine(baz);
    }
}

Extra verbose Math.Round

Expected output

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);
    }
}

Decimal is not optimized by the latest VB compiler but I think it can be.

CInt(Decimal)System.Convert.ToInt32 (similar as (int)Math.Round)

Details

  • Product in use: online
  • Version in use: e.g. 5.6.3 or a commit hash (if it's a 3rd party tool using this library, try one of the above)
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions