Golang : Converting a negative number to positive number
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
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
+7.1k Golang : Validate credit card example
+11k Golang : Create Temporary File
+28.8k Golang : Detect (OS) Operating System
+5.2k Golang : Experimental Jawi programming language
+4.9k Nginx and PageSpeed build from source CentOS example
+14.2k Golang : Convert IP version 6 address to integer or decimal number
+21.3k Golang : Create and resolve(read) symbolic links
+6.2k Golang : Extract XML attribute data with attr field tag example
+7.4k Golang : Scanf function weird error in Windows
+16.8k Golang : read gzipped http response
+15.2k Golang : How to add color to string?
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery