Golang : Check if an integer is negative or positive
A simple example on how to check if a given number is negative or positive number in Golang.
Here you go!
package main
import (
"fmt"
"math"
)
func main() {
integer := -1.0
fmt.Println("Integer is negative number :", math.Signbit(integer))
integer2 := 1.0
fmt.Println("Integer2 is negative number : ", math.Signbit(integer2))
}
Output:
Integer is negative number : true
Integer2 is negative number : false
See also : Golang : Count number of digits from given integer value
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
+6.1k Golang : A simple forex opportunities scanner
+6k Golang : Normalize email to prevent multiple signups example
+9.2k Golang : cannot assign type int to value (type uint8) in range error
+14.1k Golang : rune literal not terminated error
+10.7k Golang : Calculations using complex numbers example
+8k Golang : automatically figure out array length(size) with three dots
+12.9k Golang : Chunk split or divide a string into smaller chunk example
+14.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+13.7k Golang : Missing Bazaar command
+9.6k Swift : Convert (cast) String to Integer