Similar in concept to Math.Abs() - I'm looking for a function that when given a positive integer will return the same integer. If given a negative, will return zero.
So:
f(3) = 3 f(0) = 0 f(-3) = 0
Yes, this is simple enough to write on my own but I'm wondering if the .NET Math class already has this built in or if the same can be achieved by cleverly chaining a few Math.* calls?
Numbers in C# Numbers, in general, can be divided into two types: Integer type and floating-point types. Integer type numbers are whole numbers without decimal points. It can be negative or positive numbers.
You can use the > and < operator to check if the number is a positive or negative number in C#.
An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative. If you take an unsigned 0 and subtract 1 from it, the result wraps around, leaving a very large number (2^32-1 with the typical 32-bit integer size).
The number zero is neither positive nor negative. Positive and negative numbers are sometimes called signed numbers.
It's called Math.Max
:
Math.Max(0, x)
This seems to be what you want, no?
Math.Max(0, num);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With