Skip to content

Instantly share code, notes, and snippets.

@KEINOS
Last active April 18, 2022 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KEINOS/e915aa8c01dd438e3ffd79b05f15a4ff to your computer and use it in GitHub Desktop.
Save KEINOS/e915aa8c01dd438e3ffd79b05f15a4ff to your computer and use it in GitHub Desktop.
[Golang] Zero padded decimal string to integer / Decimal string with leading zeros to integer

[Golang] How to convert a zero-padded decimal string to an integer?

Converting decimal string with leading zeros to integer

str "000123" --> int 123

To trim a zero-padded decimal string and convert it to integer as shown above, simply use strconv.Atoi() function.

package main

import (
    "fmt"
    "strconv"
)

func ExampleStoi() {
    s := "0093"
    if i, err := strconv.Atoi(s); err == nil {
        fmt.Printf("input=%v, i=%d, type: %T\n", s, i, i)
    }

    // Output: input=0093, i=93, type: int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment