0

How can I convert following If statement using Pattern matching in switch expression?

if (Math.Round(self, decimals) - Math.Round(value, decimals) < 0)
   return -1;
else if (Math.Round(self, decimals) - Math.Round(value, decimals) == 0)
   return 0;
else
   return 1;
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
Innova
  • 334
  • 4
  • 18
  • This isn't a question about how to use Visual Studio. Please be careful not to misuse tags. Note that most have descriptions you can read to ensure you're using it for its intended purpose. – ProgrammingLlama May 19 '21 at 10:02

4 Answers4

4
return Math.Round(self, decimals) - Math.Round(value, decimals) switch
{
    < 0 => -1,
    0 => 0,
    _ => 1
};

Although this is the answer to your question in this special case there are better solutions, see other answers

Klamsi
  • 846
  • 5
  • 16
  • This is exactly what I was looking for... but I realized that there is a better solution. Thx! – Innova May 19 '21 at 10:31
4

While you could use a switch expression for this (see @Klamsi's answer), there's a better way:

return Math.Round(self, decimals).CompareTo(Math.Round(value, decimals));

From the docs:

Returns

Int32

A signed number indicating the relative values of this instance and value.

Return Value Description
Less than zero This instance is less than value, or this instance is not a number (NaN) and value is a number.
Zero This instance is equal to value, or both this instance and value are not a number (NaN), PositiveInfinity, or NegativeInfinity.
Greater than zero This instance is greater than value, or this instance is a number and value is not a number (NaN).

(Although the docs say the return value can be anything, in practice all BCL types return -1, 0 or 1).

canton7
  • 37,633
  • 3
  • 64
  • 77
  • 1
    That's better than my suggestion too! :D – ProgrammingLlama May 19 '21 at 10:08
  • Surely from what you have quoted your return values are not guaranteed to be the same? This may not be important but where the OP returned 1 your method (as I understand) could return any positive integer)... (I'll note that even if current implementations do return 1,0,-1 that the specification doesn't seem to guarantee that) – Chris May 19 '21 at 10:13
  • 1
    @Chris That's strictly true, but every implementation of `CompareTo` in the BCL returns -1, 0, or 1. People will doubtless depend on that by now, so it's not something they could ever change – canton7 May 19 '21 at 10:15
  • 1
    That's fair. I'm surprised to be honest that they don't just document it as such to make it clearer. I had wondered if some implementations might use the subtraction trick but a quick google told me why that was a terrible idea (covered in the answer to https://stackoverflow.com/questions/29343128/why-does-compareto-return-an-integer ). Anyway, I think its worth noting (even if just in comments as we have) about it in practice always returning 1,0,-1. – Chris May 19 '21 at 10:19
2

You could replace your entire code with this:

return Math.Sign(Math.Round(self, decimals) - Math.Round(value, decimals));

Math.Sign(value) documentation:

A number that indicates the sign of value, as shown in the following table.

Return Value Meaning
-1 value is less than zero.
0 value is equal to zero.
1 value is greater than zero.

I'd suggest that canton7's answer is probably the best option here though.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
0

One way is as follows:

var result = (Math.Round(self, decimals) - Math.Round(value, decimals)) switch
{
    < 0 => -1,
    0 => 0,
    _ => 1                  
};

Live example: https://dotnetfiddle.net/ly18qu

Jamiec
  • 133,658
  • 13
  • 134
  • 193