Add number parsing
This commit is contained in:
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)
|
||||
}
|
||||
Reference in New Issue
Block a user