English 中文(简体)
使用 Go 中的大整数值吗? (ParseInt 仅转换为“ 2147483647 ” )
原标题:Using big integer values in Go? (ParseInt only converts up to "2147483647"?)
  • 时间:2012-05-25 01:32:23
  •  标签:
  • go

您如何在 Go 中将一长串数字( 50 位数) 转换成整数?

下面是代码的输出:

number = 2147483647

err = strconv.ParseInt: parsing "37107287533902102798797998220837590246510135740250 ": value out of range

它似乎只能转换最多达2147483647的数字。

package main

import "fmt"
import "io/ioutil"
import "strings"
import "strconv"

var (
        number int64
)

func main() {
    fData,err := ioutil.ReadFile("one-hundred_50.txt")
    if err != nil {
            fmt.Println("Err is ",err)
        }   
    strbuffer := string(fData)
    lines := strings.Split(strbuffer, "
")

    for i, line := range lines {
        fmt.Printf("%d: %s
", i, line)
        number, err := strconv.Atoi(line)
        fmt.Println("number = ", number)
        fmt.Println("err = ", err)
    }   
}
最佳回答

您想要 < a href=> " "http://golang.org/pkg/math/big" >math/big 软件包, 提供任意精度整数支持 。

import "math/big"

func main() {
    // ...
    for i, line := range lines {
        bi := big.NewInt(0)
        if _, ok := bi.SetString(line, 10); ok {
            fmt.Printf("number = %v
", bi)
        } else {
            fmt.Printf("couldn t interpret line %#v
", line)
        }
    }
}

这里有",http://play.golang.org/p/izWUY9WwY"。

问题回答

暂无回答




相关问题
minimum work size of a goroutine [closed]

Does anyone know approximately what the minimum work size is needed in order for a goroutine to be beneficial (assuming that there are free cores for the work to be offloaded to)?

How do you get the terminal size in Go?

How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts); But how to i access TIOCGWINSZ in Go

What do you use to write Go [closed]

I know its a bit too early, but I ve been trying out Go (Google s Programming Language) and its kindof annoying to write code in gedit. So, my question: What do you use to experiment with Go?

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

热门标签