Is there a built-in function in java that would convert any negative number to a 0? what i'm wanting to do is subtract number from a variable, and ensure that it doesn't go below 0. is this possible with built-in functions or would i have to write my own?
You should use :
Math.max(0, yourVar)
You don't need a built-in function for that.
You do not need any function to turn a negative into a zero. You can use a conditional declaration of the variable and turned the negative value into a zero.
In a conditional declaration, what goes before the question mark is the condition. If the condition evaluates to true, the first value after the question mark would be assigned to the variable.
If the condition evaluates to false, the value that goes after the column will be assigned to the variable. In the case below, a would be assigned the value of itself if it does not carry a value that is below zero.
int a = -1;
a = a < 0? 0 : a;
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