Note that there are some explanatory texts on larger screens.

plurals
  1. POCleanest way to convert a `Double` or `Single` to `Integer`, without rounding
    primarykey
    data
    text
    <p>Converting a floating-point number to an integer using either <a href="https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx" rel="nofollow noreferrer">CInt</a> or <code>CType</code> will cause the value of that number to be rounded. The <code>Int</code> function and <code>Math.Floor</code> may be used to convert a floating-point number to a whole number, rounding toward negative infinity, but both functions return floating-point values which cannot be implicitly used as <code>Integer</code> values without a cast.</p> <p>Is there a concise and idiomatic alternative to <code>IntVar = CInt(Int(FloatingPointVar));</code>? Pascal included <code>Round</code> and <code>Trunc</code> functions which returned <code>Integer</code>; is there some equivalent in either the VB.NET language or in the .NET framework?</p> <p>A similar question, <em><a href="https://stackoverflow.com/questions/7684176">CInt does not round Double value consistently - how can I remove the fractional part?</a></em> was asked in 2011, but it simply asked if there was a way to convert a floating-point number to an integer; the answers suggested a two-step process, but it didn't go into any depth about what does or does not exist in the framework. I would find it hard to believe that the Framework wouldn't have something analogous to the <a href="http://en.wikipedia.org/wiki/Pascal_%28programming_language%29" rel="nofollow noreferrer">Pascal</a> <code>Trunc</code> function, given that such a thing will frequently be needed when performing graphical operations using floating-point operands [such operations need to be rendered as discrete pixels, and should be rounded in such a way that round(x)-1 = round(x-1) for all x that fit within the range of +/- (2^31-1); even if such operations are rounded, they should use Floor(x+0.5), rather than round-to-nearest-even, so as to ensure the above property]</p> <p>Incidentally, in C# a typecast from <code>Double</code> to <code>Int</code> using <code>(type)expr</code> notation uses round-to-zero semantics; the fact that this differs from the VB.NET behavior suggests that one or both languages is using its own conversion routines rather an explicit conversion operator included in the Framework. It would seem likely that the Framework should define a conversion operator? Does such an operator exist within the framework? What does it do? Is there a way to invoke it from C# and/or VB.NET?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload