Convert Double to Int

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

double DesignGlassPriceDbl =WindowAreaSqFtInt * DesignGlassPricePSF;
int DesignGlassPriceInt= System.ConvertToInt32(DesignGlassPriceDbl);

In the above code, if "DesignGlassPriceDbl" is 213.33, DesignGlassPriceInt
evaluates to 104, not 213.

How can I convert a double value to an Int? Dropping .33 in the process is
OK, but not dropping (for some reason inexplicable to me) 109.33.
 
Worked fine for me (once I inserted a dot between Convert and ToInt32).
Framework 1.1 returned 213, as expected.

Apart from the obvious question (i.e., are you sure that DesignClassPriceDbl
actually equalled 213.33 in your test), what's different about your setup?

Tom Dacon
Dacon Software Consulting
 
Stumped said:
double DesignGlassPriceDbl =WindowAreaSqFtInt * DesignGlassPricePSF;
int DesignGlassPriceInt= System.ConvertToInt32(DesignGlassPriceDbl);

In the above code, if "DesignGlassPriceDbl" is 213.33, DesignGlassPriceInt
evaluates to 104, not 213.

How can I convert a double value to an Int? Dropping .33 in the process is
OK, but not dropping (for some reason inexplicable to me) 109.33.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(I would also suggest just casting, rather than using Convert.ToInt32,
but that's a different matter.)
 
In my code prior to the first line show, I was mixing doubles and decimals,
so maybee "DesignGlassPriceDbl" was not really a double.

Anyway, I ended the mixing by making all the variables doubles and now the
code works fine.

Appararently one should not mix variables like I did and not expect trouble?

Thanks for your offer!!
 
Stumped said:
In my code prior to the first line show, I was mixing doubles and decimals,
so maybee "DesignGlassPriceDbl" was not really a double.

Anyway, I ended the mixing by making all the variables doubles and now the
code works fine.

Appararently one should not mix variables like I did and not expect trouble?

Well, it shouldn't make that kind of trouble at all - I'd still be
intrigued to see what produced those results. I'm glad it's all working
though :)
 
Back
Top