c# - If Statement for Floats? -
how make if statements using floats? code have follows:
{float p1 = float.parse(textbox1.text); if (p1 == ""){messagebox.show("home");}}
the p1 if statement on second line not work. how make proper if statement this?
edit: should explain. goal check empty box. can't use string command since want interpret numbers.
thanks in advance help.
float
values cannot "empty". if try parsing empty string float
, runtime error.
you need check string being empty before parsing, , parse more "conservative" tryparse
method not throw exception.
if (string.isnullorwhitespace(textbox1.text)) { messagebox.show("home"); } float p1; if (!float.tryparse(textbox1.text, out p1)) { messagebox.show("textbox1 not float"); }
note: in general, comparing float
s equality ==
operator not idea, because float
not exact representation. this q&a discusses problem in java, issue relevant in languages use floating point representation.
Comments
Post a Comment