Add number parsing

This commit is contained in:
Sangeeth Sudheer 2024-05-02 21:14:55 +05:30
parent f8e2fedbd2
commit 6f670fe92b
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 30 additions and 2 deletions

View File

@ -29,7 +29,8 @@ package main
// import "git.sangeeth.dev/gobyexample/json" // import "git.sangeeth.dev/gobyexample/json"
// import "git.sangeeth.dev/gobyexample/xml" // import "git.sangeeth.dev/gobyexample/xml"
// import "git.sangeeth.dev/gobyexample/time" // 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() { func main() {
// runes.Runes() // runes.Runes()
@ -61,5 +62,6 @@ func main() {
// json.Json() // json.Json()
// xml.Xml() // xml.Xml()
// time.Time() // time.Time()
rand.Rand() // rand.Rand()
numberparsing.NumberParsing()
} }

View 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)
}