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
+5.1k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+9.9k Golang : Print how to use flag for your application example
+23.2k Golang : Check if element exist in map
+20.8k Golang : Get password from console input without echo or masked
+13.6k Golang : convert rune to unicode hexadecimal value and back to rune character
+7.8k Golang : Multiplexer with net/http and map
+9.3k Golang : How to generate Code 39 barcode?
+28.8k Golang : Saving(serializing) and reading file with GOB
+19.6k Golang : How to get time from unix nano example
+7.4k Golang : Error reading timestamp with GORM or SQL driver
+11.3k Golang : Fuzzy string search or approximate string matching example
+19.8k Golang : How to get struct tag and use field name to retrieve data?