美文网首页
将输入整数按照格式分割输出

将输入整数按照格式分割输出

作者: sjyu_eadd | 来源:发表于2021-07-06 16:06 被阅读0次

    示例:
    输入:1234
    输出:1,234

    输入:211111234
    输出:211,111,234

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "strconv"
        "strings"
    )
    
    func main() {
        inputReader := bufio.NewReader(os.Stdin)
        fmt.Printf("Please input:")
        input, err := inputReader.ReadString('\n')
        if err != nil {
            fmt.Println("There were errors reading, exiting program.")
            return
        }
        fmt.Println("Your input is:", input)
        input1, err := strconv.Atoi(strings.Replace(input, "\r\n", "", -1))
        if err != nil {
            fmt.Println("strconv.Atoi error, exiting program.")
            return
        }
        var result string
        for input1 > 0 {
            if input1 / 1000 == 0 {
                result = strconv.Itoa(input1 % 1000) + result
            } else {
                result = "," + strconv.Itoa(input1 % 1000) + result
            }
            input1 /= 1000
        }
        fmt.Println("convert result:", result)
    }
    

    相关文章

      网友评论

          本文标题:将输入整数按照格式分割输出

          本文链接:https://www.haomeiwen.com/subject/yvpgultx.html