Add number parsing
This commit is contained in:
parent
f8e2fedbd2
commit
6f670fe92b
6
main.go
6
main.go
@ -29,7 +29,8 @@ package main
|
||||
// import "git.sangeeth.dev/gobyexample/json"
|
||||
// import "git.sangeeth.dev/gobyexample/xml"
|
||||
// import "git.sangeeth.dev/gobyexample/time"
|
||||
import "git.sangeeth.dev/gobyexample/rand"
|
||||
// import "git.sangeeth.dev/gobyexample/rand"
|
||||
import "git.sangeeth.dev/gobyexample/numberparsing"
|
||||
|
||||
func main() {
|
||||
// runes.Runes()
|
||||
@ -61,5 +62,6 @@ func main() {
|
||||
// json.Json()
|
||||
// xml.Xml()
|
||||
// time.Time()
|
||||
rand.Rand()
|
||||
// rand.Rand()
|
||||
numberparsing.NumberParsing()
|
||||
}
|
||||
|
26
numberparsing/numberparsing.go
Normal file
26
numberparsing/numberparsing.go
Normal file
@ -0,0 +1,26 @@
|
||||
package numberparsing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func NumberParsing() {
|
||||
var expected int32 = (1 << 31) - 1
|
||||
parsed, _ := strconv.ParseInt(fmt.Sprint(expected), 10, 32)
|
||||
if expected != int32(parsed) {
|
||||
panic(fmt.Sprintf("Expected %d but got %d parsed=%d", expected, int32(parsed), parsed))
|
||||
}
|
||||
|
||||
num, _ := strconv.ParseInt("0xff", 0, 64)
|
||||
fmt.Println("Hex num =>", num)
|
||||
|
||||
num, _ = strconv.ParseInt("0777", 0, 64)
|
||||
fmt.Println("Octal num =>", num)
|
||||
|
||||
num, _ = strconv.ParseInt("0b11", 0, 64)
|
||||
fmt.Println("Binary num =>", num)
|
||||
|
||||
fNum, _ := strconv.ParseFloat("3.1415", 64)
|
||||
fmt.Println("Float num =>", fNum)
|
||||
}
|
Loading…
Reference in New Issue
Block a user