gobyexample/numberparsing/numberparsing.go

27 lines
612 B
Go
Raw Permalink Normal View History

2024-05-02 15:44:55 +00:00
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)
}