Narrowing Conversion - Double to int32

  • Thread starter Thread starter Terry
  • Start date Start date
T

Terry

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
 
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 said:
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.
 
Thanks for the quick reply Amdrit,
I'll see what results I get.
Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top