LoginSignup
20
10

More than 3 years have passed since last update.

【Go】int64型・uint64型からstring型へ変換する方法

Last updated at Posted at 2020-01-23

int64型からstring型へ変換する方法

// int64
var Int64 int64 = 9223372036854775807

// strconv.FormatInt(int64, 基数(10進数))
convertedInt64 := strconv.FormatInt(Int64, 10)

9223372036854775807int64の最大数です

int64 is the set of all signed 64-bit integers. Range: -9223372036854775808 through 9223372036854775807.

uint64型からstring型へ変換する方法

// uint64   
var Uint64 uint64 = 18446744073709551615

// strconv.FormatUint(uint64, 基数(10進数))
convertedUint64 := strconv.FormatUint(Uint64, 10)

18446744073709551615はuint64の最大数です

uint64 is the set of all unsigned 64-bit integers. Range: 0 through 18446744073709551615.

Go Playground

package main

import (
    "strconv"
    "fmt"
    "reflect"
)

func main() {

    var Int64 int64 = 9223372036854775807
    convertedInt64 := strconv.FormatInt(Int64,10)
    fmt.Printf("%v with type %s \n", convertedInt64, reflect.TypeOf(convertedInt64))

    var Uint64 uint64 = 18446744073709551615
    convertedUint64 := strconv.FormatUint(Uint64,10)
    fmt.Printf("%v with type %s \n", convertedUint64, reflect.TypeOf(convertedUint64))
}

実行結果

9223372036854775807 with type string 
18446744073709551615 with type string 

【おまけ】string型からint64・uint64型へ変換からの再びstring型へ変換

package main

import (
    "strconv"
    "fmt"
    "reflect"
)

func main() {
    fmt.Println("\n----- convert String to Int64 and Uint64 -------------------")

    // int64
    var strInt64 string = "9223372036854775807"
    convertedStrInt64, _ := strconv.ParseInt(strInt64, 10, 64)

    fmt.Printf("%v with type %s \n", convertedStrInt64, reflect.TypeOf(convertedStrInt64))

    // uint64   
    var strUint64 string = "18446744073709551615"
    convertedStrUint64, _ := strconv.ParseUint(strUint64, 10, 64)

    fmt.Printf("%v with type %s \n", convertedStrUint64, reflect.TypeOf(convertedStrUint64))

    fmt.Println("\n----- convert Int64 and Uint64 to String -------------------")

    convertedInt64 := strconv.FormatInt(convertedStrInt64,10)
    fmt.Printf("%v with type %s \n", convertedInt64, reflect.TypeOf(convertedInt64))

    convertedUint64 := strconv.FormatUint(convertedStrUint64,10)
    fmt.Printf("%v with type %s \n", convertedUint64, reflect.TypeOf(convertedUint64))
}

実行結果

----- convert String to Int64 and Uint64 -------------------
9223372036854775807 with type int64 
18446744073709551615 with type uint64 

----- convert Int64 and Uint64 to String -------------------
9223372036854775807 with type string 
18446744073709551615 with type string 
20
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
10