Is there a way to simplify this ternary expression?

112 Views Asked by At

I have to convert a number from a string to double, but it should be a max of 15. This is my code to do this at the moment:

var num = double.Parse(stringNum, CultureInfo.InvariantCulture);
num = num <= 15 ? num : 15;

Is there a way to do this in a single expression?

1

There are 1 best solutions below

0
AbdelAziz AbdelLatef On BEST ANSWER

Do it like this:

double num = Math.Min(double.Parse(stringNum, CultureInfo.InvariantCulture), 15);