# Make a negative number positive

## Recommended Posts

This maybe is a stupid question, but I'm trying to turn a negative number (like -3, -0.15, -18) into a positive value (like 3, 0.15, 18).

For instance:

```\$var[5][2] = [[0.18, "Johnny"][-6, "Elsa"][2, "Robby"][0.57, "Esther"][-0.29, "Marcus"]]

_Arraysort(\$var, 0, 0, 0, 2)```

I want extract the name that's closest to 0, however if I sort in this order, "Elsa" will be \$var[0][0] and I want "Johnny" to become \$var[0][0].

This is why I want to make the negative values positive ("elsa" and "marcus"), but I'm kinda stuck on how to do this the simplest way.

##### Share on other sites

You are looking for Abs() (Absolute Value)

```\$Negative = Random(-100, -1,1)
\$Positive = Abs(\$Negative)
Msgbox(0,"", "Negative: "&\$Negative&@CRLF&"Positive: "&\$Positive)```
Edited by Paulie

##### Share on other sites

Paul, what do you think you're doing, answering my easy questions.

##### Share on other sites

The same without using the Abs()..

```\$Negative = Random(-100, -1,1)
\$Positive = -\$Negative
Msgbox(0,"", "Negative: "&\$Negative&@CRLF&"Positive: "&\$Positive)```

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

My Work...

Spoiler

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating )

* === My topics === *

==================================================

==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

##### Share on other sites

The same without using the Abs()..

```\$Negative = Random(-100, -1,1)
\$Positive = -\$Negative
Msgbox(0,"", "Negative: "&\$Negative&@CRLF&"Positive: "&\$Positive)```

This is NOT the same as abs().

##### Share on other sites

```if \$number < 0 then
\$numberDoubleNegative = 2 * \$number
\$numberPositified = \$number - \$numberDoubleNegative
\$number = \$numberPositified
endif```

Whoa I just won AutoIt

##### Share on other sites

Cusem - this works to do what you want:

```#Include <Array.au3>

;Dim \$var[5][2] = [["0.18", "Johnny"], ["-6", "Elsa"], ["2", "Robby"], ["0.57", "Esther"], ["-0.29", "Marcus"]]
Dim \$var[5][2] = [[0.18, "Johnny"], [-6, "Elsa"], [2, "Robby"], [0.57, "Esther"], [-0.29, "Marcus"]]
For \$i = 0 To UBound(\$var) - 1
;\$var[\$i][0] = String(Abs(Number(\$var[\$i][0])))
\$var[\$i][0] = Abs(\$var[\$i][0])
Next

_Arraysort(\$var, 0, 0, 0, 2)
_ArrayDisplay(\$var)```

I'm not sure of the wisdom of mixing strings with numbers in an array, but this code seems to work. I initially changed the numbers in you defined array into strings and used the rem'd - out line to "ABS" the data.

Here is from the help file under AutoIt > Language Reference > Variables:

An Array is a variable containing series of data elements of the same type and size....

It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays: ...

This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array. Especially the use of an Array inside another Array will severely affect the execution speed of your script.

Edited by Squirrely1

##### Share on other sites

This is NOT the same as abs().

I didn't say that it is

I only say that you not have to use Abs() to get positive number.

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

My Work...

Spoiler

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating )

* === My topics === *

==================================================

==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

##### Share on other sites

Thanks guys!

This was exactly what I was looking for!

##### Share on other sites

HI,

can also do a * -1

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

##### Share on other sites

Or:

`sqrt(\$num ^ 2)`

##### Share on other sites

That's probably the fastest outside of Abs. But then you will lose data if the number is large.

##### Share on other sites

Another Abs() equivalent

\$num * ((-1)^(\$num<0))

,where \$num is positive or negative.

Only took 11days to think of it.

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...