Go言語
Go言語で数値を文字列に変換する
数値型を文字列に変換(フォーマット)します。型がわからないときには、fmt.Sprint()を使うと良いかもしれません。
from_num_to_string.go
package main import ( "fmt" "strconv" ) func main() { fmt.Println("for int, use Itoa: " + strconv.Itoa(100)) fmt.Println("for bool, use FormatBool: " + strconv.FormatBool(true)) fmt.Println("for int64, use FormatInt: " + strconv.FormatInt(100, 10)) fmt.Println("for float64, use FormatFloat: " + strconv.FormatFloat(100.001, 'e', 2, 64)) fmt.Println("for FormatInt, we can specify base: " + strconv.FormatInt(100, 2)) fmt.Println("FormatUint also available: " + strconv.FormatUint(100, 10)) fmt.Println("") fmt.Println("If you don't know type of number, you can use fmt.Sprint()") fmt.Println("Sprint with Integer: " + fmt.Sprint(100)) fmt.Println("Sprint with float: " + fmt.Sprint(100.1)) }
実行結果
$ go run from_num_to_string.go for int, use Itoa: 100 for bool, use FormatBool: true for int64, use FormatInt: 100 for float64, use FormatFloat: 1.00e+02 for FormatInt, we can specify base: 1100100 FormatUint also available: 100 If you don't know type of number, you can use fmt.Sprint() Sprint with Integer: 100 Sprint with float: 100.1