Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 481 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby: Change negative number to positive number?

#1
What's the simplest way of changing a negative number to positive with ruby?

ie. Change "-300" to "300"
Reply

#2
Most programming languages have the ABS method, however there are some that do not
Whilst I have not used Ruby before, I am familiar its a framework that runs on PHP

The abs method is available on PHP

[To see links please register here]


With Ruby the syntax appears slightly different is integer.abs

[To see links please register here]


But for future reference the abs method is really small to code your self.

here is how in a few different languages:

JavaScript:

function my_abs(integer){
if (integer < 0){
return integer * -1;
}
return interger;
}



Python:

def my_abs(integer):
if (integer < 0):
return integer * -1
return integer


c:

int my_abs(int integer){
if (interger < 0){
return integer * -1;
}
return integer;
}


This means should you ever find yourself with a programming language that doesnt have a built in abs method, you know how to code your own its just simply multiply any negative number by -1 as you would of gathered in my examples
Reply

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

x * -1

That way you can go back and forth.
Reply

#4
Using `abs` will return the [absolute value](

[To see links please register here]

) of a number

-300.abs # 300
300.abs # 300
Reply

#5
Put a negative sign in front of it.

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through