An elegant way to negate a numeric value based on a boolean value

I have a decimal variable that I would like to deny if the boolean variable is true. Can anyone think of a more elegant way to do this than this:

decimal amount = 500m; bool negate = true; amount *= (negate ? -1 : 1); 

I think something along the lines of bitwise operators or strictly mathematical implementation.

+4
source share
7 answers

Personally, I would simply use the if statement, since I believe this is most clear in terms of intent:

 decimal amount = 500m; bool negate = true; // ... if (negate) amount *= -1; 

In fact, this is not an extra typification (it is actually shorter!), And more clear, in my opinion.

+13
source

Use the decimal unary negation operator (as you already do):

 using System; class Program { static void Main() { bool negate = true; decimal test = 500M; Console.WriteLine(negate == true ? -test : test); } } 

Output:

 -500 

Honestly, this is much clearer and better than multiplying by -1 in such a strange way.

+2
source

Another shot at math wizards?

How about making your existing solution a little more readable, but still use the instructions? true: false shortcut?

Your decision was:

 amount *= (negate ? -1 : 1); 

Perhaps refactoring to

 amount = (negate ? amount*-1 : amount); 

To add even more readability to your code, you can create a reuse class that handles these things for you:

 public static class MathHelpers() { // Negates the result if shouldNegate is true, otherwise returns the same result public static decimal Negate(decimal value, bool shouldNegate) { // In this black-box solution you can use "fancier" shortcuts return value *= negate ? -1 : 1; } } 

And in your other code, you now have a very readable function to use ...

 decimal amount = 500m; bool negate = true; amount = MathHelper.Negate(amount, negate); 

In general, although I agree that elegance and readability live in the same trolley, not different:

 if (condition) output *= -1; 

more readable than

 value *= condition ? -1 : 1; 
+2
source
 public static decimal Negate(this decimal value, bool isNegate){ if(isNegate) return value * -1; return value; } 

Make the extension method decimal. Easy to use.

name amount.Negate(negate)

+1
source

This already exists since Framework 1.1:

System.Decimal.Negate Method

public static decimal Negate (decimal e)

Usage example:

 decimal amount = 500m; bool negate = true; if(negate) amount = decimal.Negate(amount); // amount now holds -500 // Use amount 
+1
source

If your negate flag negate based on some numerical value, you can use Math.Sign , which is the most β€œmathematical” way I can think of.

 double negationValue = -45.0; amount *= Math.Sign(negationValue); 

or in the boolean case just (not very elegant):

 amount *= Math.Sign(0.5 - Convert.ToByte(negate)); 
0
source
 amount *= Math.Pow(-1, Convert.ToInt32(negate)) 

This is the assumption that casting to boolean in C # will result in 0 for false and a 1 for true. However, I do not think this is elegant, since it is confusing.

edit: converted to int

-1
source

All Articles