Reputation: 3
I'm trying to solve a data mystery here but i'm not sure whether it is my code issue or internet browser issue... it works fine in IE6 and IE8 but somehow the data appears incorrectly when it was used by customers who may not be using IE...
decimal? a = 1.0000m;
decimal? b = 0.6999m;
decimal? c = null;
string aDesc = "";
string bDesc = "";
string cDesc = "";
if (a >= (Decimal).8)
aDesc = "condition A achieved";
if (b >= (Decimal).8)
bDesc = "condition B achieved";
if (c >= (Decimal).8)
cDesc = "condition C achieved";
Does all the strings get assigned at the end of this in all browsers? Thanks.
Upvotes: 0
Views: 365
Reputation: 19044
Note: instead of (Decimal).8
you can use .8m
. It's shorter and a bit cleaner.
Comparing a null (as in your Nullable) in this situation results in false in every case. Try this in a fresh project:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
decimal? a = null;
if (a < .8m)
{
MessageBox.Show("Less Than");
}
else if (a >= .8m)
{
MessageBox.Show("Greater Than or equal to");
}
else
{
MessageBox.Show("Neither");
}
}
}
If you think about it, it makes sense. null
means an absence of value, which is very different from none (or zero), which are specific values. Comparison against an absence of something has no real definition, it is not larger, nor is is smaller, so both resolve to false. Is a non-existent person taller than me? Nope, he doesn't exist. Is a non-existent person shorter than me? Nope, he doesn't exist.
EDIT
Also, you'll find that your second condition (as pointed out by others) is always flat out false.
/EDIT
That's what you're seeing, not a browser issue, hope that helps!
Upvotes: 1
Reputation: 12604
You're running C#, which should be executed on the server side. It should be browser independent.
Also, only the first condition is true. The first value is greater than .8, the second is clearly not greater, and I believe nullable types on a comparison will always return false.
Upvotes: 2