473,473 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Narrowing Conversion - Double to int32

I have x as double, hh as int32

in VBA I can perfom hh=Int(x) (narrowing conversion but disallowed in .NET)

One example in .NET is hh = System.Convert.ToInt32(x)

Could I use IConvertible and how would I code this?

Which is the better method keeping in mind that I would also be coding
calculations in hours/minutes/seconds, degress/minutes/seconds, radians and
logarithms. I would not want to lose too much precision in my results.

Regards
Jan 11 '07 #1
4 1937
In .Net doubles are not precise, I recommend decimals. If you have option
strict enabled then:

Dim x a decimal, hh as integer

hh = ctype(int(x),integer)
or
hh = ctype(x,integer)
or
if not integer.tryparse(x,hh) then msgbox("Cannot parse.")
"Terry" <yo**********@REMOVETHISntlworld.comwrote in message
news:et**************@TK2MSFTNGP06.phx.gbl...
>I have x as double, hh as int32

in VBA I can perfom hh=Int(x) (narrowing conversion but disallowed in
.NET)

One example in .NET is hh = System.Convert.ToInt32(x)

Could I use IConvertible and how would I code this?

Which is the better method keeping in mind that I would also be coding
calculations in hours/minutes/seconds, degress/minutes/seconds, radians
and logarithms. I would not want to lose too much precision in my results.

Regards

Jan 11 '07 #2
Thanks for the quick reply Amdrit,
I'll see what results I get.
Regards
"AMDRIT" <am****@hotmail.comwrote in message
news:ey*************@TK2MSFTNGP04.phx.gbl...
In .Net doubles are not precise, I recommend decimals. If you have option
strict enabled then:

Dim x a decimal, hh as integer

hh = ctype(int(x),integer)
or
hh = ctype(x,integer)
or
if not integer.tryparse(x,hh) then msgbox("Cannot parse.")
"Terry" <yo**********@REMOVETHISntlworld.comwrote in message
news:et**************@TK2MSFTNGP06.phx.gbl...
>>I have x as double, hh as int32

in VBA I can perfom hh=Int(x) (narrowing conversion but disallowed in
.NET)

One example in .NET is hh = System.Convert.ToInt32(x)

Could I use IConvertible and how would I code this?

Which is the better method keeping in mind that I would also be coding
calculations in hours/minutes/seconds, degress/minutes/seconds, radians
and logarithms. I would not want to lose too much precision in my
results.

Regards


Jan 11 '07 #3
"Terry" <yo**********@REMOVETHISntlworld.comschrieb:
>I have x as double, hh as int32

in VBA I can perfom hh=Int(x) (narrowing conversion but disallowed in
.NET)

One example in .NET is hh = System.Convert.ToInt32(x)

Could I use IConvertible and how would I code this?

Which is the better method keeping in mind that I would also be coding
calculations in hours/minutes/seconds, degress/minutes/seconds, radians
and logarithms. I would not want to lose too much precision in my results.
Check out 'CInt' too, which is maybe what you are looking for.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 12 '07 #4
OK thanks for that one.
Regards
Terry

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:uk**************@TK2MSFTNGP06.phx.gbl...
"Terry" <yo**********@REMOVETHISntlworld.comschrieb:
>>I have x as double, hh as int32

in VBA I can perfom hh=Int(x) (narrowing conversion but disallowed in
.NET)

One example in .NET is hh = System.Convert.ToInt32(x)

Could I use IConvertible and how would I code this?

Which is the better method keeping in mind that I would also be coding
calculations in hours/minutes/seconds, degress/minutes/seconds, radians
and logarithms. I would not want to lose too much precision in my
results.

Check out 'CInt' too, which is maybe what you are looking for.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Erik Frey | last post by:
Hello, Say I have the following two classes: class Base { } class Inherited : Base {
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
2
by: Bill Nguyen | last post by:
What do I need to do to add a row to a table with single column? I got the "mnarrowing conversion" ewrror mesage. Thanks Bill -----------------
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
2
by: jason | last post by:
Since going to framework 2.0 from 1.1, I'm getting error: Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: On line: Dim LogInfo As New...
4
by: jaime | last post by:
Hi all. Apologies, since this is more a tool question, than strictly a language question, but hey, it seemed like an appropriate place to ask... I'm a c newbie (and have been now for about 6...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I have a strange situation with vb 2005 regarding option strict that looks like a compiler bug to me. Consider: Dim f1 As New Font("Arial", 8.25) Dim f2 As New Font("Arial", 8.25,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
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...
0
agi2029
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,...
1
isladogs
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.