how do i make my label text something depending if the value is negative or positive

how do i make my label text something depending if is negative or positive value it gets from resultat7 in C#

 samenligninglabel.Content = "Det nye dæk er " + Environment.NewLine
     + resultat7.ToString() + "%" + text1 if negative otherwise text2 if positive
     + Environment.NewLine + "end det nuværende";

Upvotes: 0

Views: 396

Answers (3)

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

Negative and Positive numbers are, by definition, less than or greater than 0, respectively.

So, just check whether the value is greater than or less than zero:

isPositive = (resultat7 > 0);

Then you can use the ternary conditional operator or an if-statement to assign content to your label:

samenligninglabel.Content = isPositive ? "Positive" : "Negative"; // Change to required text.

Or:

if (isPositive)
{
    samenligninglabel.Content = "Positive";
}
else
{
    samenligninglabel.Content = "Negative";
}

Obviously, the first version is more compact and thus better.

Upvotes: 1

M B
M B

Reputation: 2326

check if its greater or less than 0

(resultat7 < 0) ? "negative text" : "positive text"

Upvotes: 0

Marcin Iwanowski
Marcin Iwanowski

Reputation: 791

If resultat7 is boolean variable you can use following code:

 resultat7 ? text2 : text1

Upvotes: 0

Related Questions