Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Change negative number to positive number?

Tags:

math

numbers

ruby

People also ask

How do you make a number negative in Ruby?

You can create one by writing it: 123 is Ruby code and it represents the number one-hundred-twenty-three. Negative numbers are created by prepending a minus - : This is the number minus-ninety-nine: -99 . And of course there are decimal numbers, too. Again, you create one by writing it: 12.34 .

How do I turn a negative number positive in Python?

In Python, positive numbers can be changed to negative numbers with the help of the in-built method provided in the Python library called abs (). When abs () is used, it converts negative numbers to positive. However, when -abs () is used, then a positive number can be changed to a negative number.

How do you turn a negative into a positive in Java?

To convert negative number to positive number (this is called absolute value), uses Math. abs(). This Math. abs() method is work like this “ number = (number < 0 ? -number : number); ".


Using abs will return the absolute value of a number

-300.abs  # 300
300.abs   # 300

Put a negative sign in front of it.

>> --300
=> 300
>> x = -300
=> -300
>> -x
=> 300

Wouldn't it just be easier to multiply it by negative one?

x * -1

That way you can go back and forth.