I'm trying to do a simple picee of maths where I work out if a value is between two values and if so, it does, it should do a simple division. However sometimes the value divided by are like 0.9, 0.6 etc and that always returns 0.
in this example,
int m_LocationSqrMtr = 4339;
float m_DefaultPricing = Convert.ToSingle(DefaultPricing);
float m_manDays;
if (m_LocationCosts > 450 && m_LocationCosts < 700)
{
m_DefaultPricing = 700 / m_LocationSqrMtr;
}
My guess is that the type of m_LocationSqrMtr
is int
, in which case this expression:
700 / m_LocationSqrMtr
... will be computed using integer arithmetic, and the result converted to float
. I suspect you want:
if (m_LocationCosts > 450 && m_LocationCosts < 700)
{
m_DefaultPricing = 700f / m_LocationSqrMtr;
}
The f
suffix on the literal means that it's a float
literal, so first m_LocationSqrMtr
will be promoted to float
, and then the division performed using float
arithmetic.
However, if this is meant to be representing currency values, you should consider using decimal
instead of float
- and then probably rounding the value to 2 decimal places. If you do all your currency arithmetic in decimal
, you're less likely to run into unexpected results...
See more on this question at Stackoverflow