Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for .NET Math method that will zero a negative integer

Tags:

c#

.net

math

vb.net

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?

like image 484
Paul Sasik Avatar asked Mar 24 '10 16:03

Paul Sasik


People also ask

Can an int be a negative number in C#?

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.

How do I get negative numbers in C#?

You can use the > and < operator to check if the number is a positive or negative number in C#.

Can an integer be negative in coding?

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).

Is zero considered negative or positive?

The number zero is neither positive nor negative. Positive and negative numbers are sometimes called signed numbers.


2 Answers

It's called Math.Max:

Math.Max(0, x) 
like image 82
Bubblewrap Avatar answered Oct 05 '22 22:10

Bubblewrap


This seems to be what you want, no?

Math.Max(0, num); 
like image 23
Ron Warholic Avatar answered Oct 05 '22 23:10

Ron Warholic