We've been doing a little experimenting and it seems VB.NET doesn't
have a direct equivalent to a C# double to integer cast.
Dim d as Double = 2.5#
Dim i as Integer = CType(d, Integer)
Is decompiled to
double d = 2.5;
int i = (int)Math.Round (d);
and
Dim d as Double = 2.5#
Dim i as Integer = CType(Math.Floo r(d),Integer)
Is decompiled to
double d = 2.5;
int i = (int)Math.Round (Math.Floor(d)) ;
So how the heck can you do a straight cast in VB.NET? DirectCast
doesn't work on value types and the CType functions always add a
Math.Round call. CInt(d) gives same results as CType(d, Integer).
The Convert.ToInt32 () function also does rounding.
Thanks,
Sam 33 9336
If you get 2 in your integer then is nothing wrong because integer is not
supposed to have decimal.
chanmm
"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:a3******** *************** *********@4ax.c om... We've been doing a little experimenting and it seems VB.NET doesn't have a direct equivalent to a C# double to integer cast.
Dim d as Double = 2.5# Dim i as Integer = CType(d, Integer)
Is decompiled to
double d = 2.5; int i = (int)Math.Round (d);
and
Dim d as Double = 2.5# Dim i as Integer = CType(Math.Floo r(d),Integer)
Is decompiled to
double d = 2.5; int i = (int)Math.Round (Math.Floor(d)) ;
So how the heck can you do a straight cast in VB.NET? DirectCast doesn't work on value types and the CType functions always add a Math.Round call. CInt(d) gives same results as CType(d, Integer).
The Convert.ToInt32 () function also does rounding.
Thanks,
Sam
Hi
I think this is VB.NET's feature.
Conversions that are mostly pure IL statements. The best example here is
the conversion from Double to Integer. This compiles down to a call to
Math.Round and then a conv.i4.ovf instruction. The purpose of the extra
call is to implement banker's rounding, which the CLR doesn't natively
support. (In banker's rounding, a decimal number equidistant between two
whole numbers is rounded to the nearest even number. So 3.5 rounds to 4,
but 2.5 rounds to 2.) In this case, calling Convert.ToInt32 (Double) is not
the same as CInt(Double), and you have to choose which semantic you desire.
Conversion operators in VB http://www.panopticoncentral.net/arc...6/07/1200.aspx
Best regards,
Perter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
But doesn't adding that Math.Round operation remove our options on how
to handle conversions and add extra unnecessary function calls and
processing? Certainly for someone working in both languages, it's
very confusing that a cast in C# does something different than a
"cast" in VB.NET.
Furthermore, the additional round operation adds extra functionality
and processing time that may be totally unnecessary. Take for example
this code:
C#:
int i = (int)2.5#;
VB.NET
Dim i as Integer = CType(Math.Floo r(2.5),Integer)
In the C# case it's a cast so we know the decimal will be truncated
and i will have 2. in VB.NET, we have to know that CType does weird
non-standard casting behavior by adding a round function and therefore
have to call Floor ourselves before doing the conversion.
Furthermore, since we called Math.Floor() the call to Math.Round()
which is added by the compiler is totally useless--the result from
Math.Floor is already integral, it's just stored as a floating point
value.
Sam
On Wed, 26 Jan 2005 01:45:34 GMT, v-******@online.m icrosoft.com
("Peter Huang" [MSFT]) wrote: Hi
I think this is VB.NET's feature.
Conversions that are mostly pure IL statements. The best example here is the conversion from Double to Integer. This compiles down to a call to Math.Round and then a conv.i4.ovf instruction. The purpose of the extra call is to implement banker's rounding, which the CLR doesn't natively support. (In banker's rounding, a decimal number equidistant between two whole numbers is rounded to the nearest even number. So 3.5 rounds to 4, but 2.5 rounds to 2.) In this case, calling Convert.ToInt32 (Double) is not the same as CInt(Double), and you have to choose which semantic you desire.
Conversion operators in VB http://www.panopticoncentral.net/arc...6/07/1200.aspx
Best regards,
Perter Huang Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
The only way I could find to get rid of the call to Math.Round was to
use Convert.ToInt32 instead, but the IL was still not the same as the
C# code. I turned integer overflow checsk off as well.
"Samuel R. Neff" <bl****@newsgro up.nospam> schrieb: Furthermore, the additional round operation adds extra functionality and processing time that may be totally unnecessary. Take for example this code:
C#:
int i = (int)2.5#;
VB.NET
Dim i as Integer = CType(Math.Floo r(2.5),Integer)
I think the VB language designers weighted cost and value of this feature,
and then decided to add it to VB. I have never heard any complains about VB
rounding implicitly the way it does, so I would conclude that this
discussion is rather academical.
Just my 2 Euro cents...
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Hi Samuel,
I agree with Herfried's suggestion.
Based on my understanding, although VB and C# are essentially language
alternatives for writing .NET applications. There is still a little that
differentiates the two on a feature by feature as well as development
experience basis. Over time, VB and C# will diverge, and each will
continue to develop their own personalities specifically targeted toward
their respective customer bases.
VB will not match C# feature by feature, although quite often, features
will appear in both. VB will differentiate on the low end, while C# will
differentiate on the high end. VB will have more features targeted at
simplifying application development, while C# will have power features
which may be complicated to use and understand. From a development
experience perspective, VB will focus on designing applications and adding
code secondarily, while C# will probably focus on writing code first.
Best regards,
Perter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Samuel,
I agree with Herfried's suggestion.
Based on my understanding, although VB and C# are essentially language
alternatives for writing .NET applications. There is still a little that
differentiates the two on a feature by feature as well as development
experience basis. Over time, VB and C# will diverge, and each will
continue to develop their own personalities specifically targeted toward
their respective customer bases.
VB will not match C# feature by feature, although quite often, features
will appear in both. VB will differentiate on the low end, while C# will
differentiate on the high end. VB will have more features targeted at
simplifying application development, while C# will have power features
which may be complicated to use and understand. From a development
experience perspective, VB will focus on designing applications and adding
code secondarily, while C# will probably focus on writing code first.
Best regards,
Perter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Peter,
Is that the reason that
dim a as double = 99/50 *1
is more precise than
double a = 99/50 * 1;
:-)
As well can you tell me what you mean with this VB will differentiate on the low end, while C# will differentiate on the high end.
I think that it would be in your message, "The attitude from a VBNet
programmer is often that he starts using the designer tools provided by
Visual Studio Net to become productive, while the C# programmer has often an
attitude to ignore those".
Or did you mean something else?
Cor
Peter,
Is that the reason that
dim a as double = 99/50 *1
is more precise than
double a = 99/50 * 1;
:-)
As well can you tell me what you mean with this VB will differentiate on the low end, while C# will differentiate on the high end.
I think that it would be in your message, "The attitude from a VBNet
programmer is often that he starts using the designer tools provided by
Visual Studio Net to become productive, while the C# programmer has often an
attitude to ignore those".
Or did you mean something else?
Cor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: dataangel |
last post by:
I wrote a function to compare whether two strings are "similar" because
I'm using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like "inevtory" and the like. To
save time the first thing my function does is check if I'm comparing an
empty vs. a non-empty string, because they are to be never considered
similar. Right now I have to write out the check like this:
if str1 and str2:
if...
|
by: Aaron W. West |
last post by:
Timings... sometimes there are almost too many ways to do the same thing.
The only significant findings I see from all the below timings is:
1) Integer math is generally fastest, naturally. Bigint math isn't much
slower, for integers that all fit within an integer.
2) Converting float to varchar is relatively slow, and should be avoided if
possible. Converting from integer to varchar or varchar to int is several
times faster.
|
by: Oenone |
last post by:
Is it possible to create an object which can have methods and properties,
but which can also be treated as a string?
I'm trying to create a wrapper around the IIS Request.Form object which
behaves the same as the classic ASP object behaved. This will allow me to
quickly get a large amount of code up and running (I can then tweak it to
not require this wrapper at a more leisurely pace).
When queried directly, this object returns a...
|
by: mark |
last post by:
I know I am being incredibly dunderheaded about this consider the following:
STRICT ON
Dim a(,) As Double = {{1, 2}, {3, 4}, {5, 6}}
Private Sub T1(ByVal a As Array)
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
a(i, j) = 2 * a(i, j) Underlined as latebinding error
Next
Next
|
by: rz0 |
last post by:
Hi all,
This is a question about both C89 and C99 and is based on my partial
reading of the standard drafts (one from before C89 but mainly
N1124). If appropriate, please give a separate answer for each version
of the language.
Let's consider the conversion from a given floating real type to a
specific integer type.
| |
by: Rouben Rostamian |
last post by:
Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.
The prototype of the function umfpack_di_symbolic() is:
int umfpack_di_symbolic
(
int n_row,
int n_col,
|
by: Martin Jørgensen |
last post by:
Hi,
Short question:
Any particular reason for why I'm getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MAX;
|
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
by: raylopez99 |
last post by:
The below did not set off my compiler, perhaps because I set the
warnings to a higher (less nag) level.
Strange.
FYI, no question being asked.
RL
double d2030;
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |