# Golang : Converting a negative number to positive number

Tags : golang math-abs negative positive convert

Helping out a friend here to figure out why her totalizer formula is spitting out incorrect result. Apparently, what she wanted to achieve is to convert a negative number to positive number so that her totalizer formula will calculate properly.

According to her co-workers, there are times when the field instrument will pump out negative number (flow rate), but it should be assumed to be a positive number and the numbers from a few measurement instruments have to be added up.

Sometimes, the simplest thing can be hardest to spot and we just assume the final calculation is correct. Writing this down as a reminder on how to convert a negative number to positive number properly with `math.Abs()`

function.

Not going to show you the actual formula in totaling up the flow rates, but a simple example will do.

Here you go!

```
package main
import (
"fmt"
"math"
)
func main() {
total := 1 + 1 + (-1)
fmt.Println("Total : ", total)
correctTotal := 1 + 1 + math.Abs(-1) // convert negative 1 to positive
fmt.Println("Correct total with absolute : ", correctTotal)
}
```

Output:

Total : 1

Correct total with absolute : 3

#### See also : Golang : Calculate BMI and risk category

Tags : golang math-abs negative positive convert

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+2.3k Golang : Spell checking with ispell example

+2.9k Golang : Get expvar(export variables) to work with multiplexer

+2.7k Golang : How to determine if request or crawl is from Google robots

+4k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

+2.7k Golang : Quadratic example

+1.5k Golang : Find correlation coefficient example

+2.3k Golang : Detect face in uploaded photo like GPlus

+6.9k Golang : Defer function inside init()

+4.9k Golang : Submit web forms without browser by http.PostForm example

+1.2k Javascript : Change page title to get viewer attention

+4.9k Golang : Get all local users and print out their home directory, description and group id

+19.5k Golang : Call a function after some delay(time.Sleep and Tick)