TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang ParseInt Examples: Convert String to Int

Use the strconv package and the ParseInt and Atoi funcs. Convert strings to ints.
ParseInt. A string contains digit characters like "123." We can convert this string to a number (an int) with the strconv.ParseInt method.
Other options. With Atoi we have a simpler way to call ParseInt. These methods return 2 values: the parsed integer itself, and an error code.
Example, ParseInt. Let us begin with this simple example. We import the strconv package at the top. We assign 2 values to the result of strconv.ParseInt.

Argument 1: We call ParseInt with 3 arguments—the first is the string we want to get an integer from.

Argument 2: This is the base of the string being parsed. Most numbers we use are base 10.

Argument 3: This is the bit size of the resulting int. 0 means int, while 6 (for example) means int16, with 16 bits.

Golang program that uses strconv.ParseInt package main import ( "fmt" "strconv" ) func main() { value := "123" // Convert string to int. number, _ := strconv.ParseInt(value, 10, 0) // We now have an int. fmt.Println(number) if number == 123 { fmt.Println(true) } } Output 123 true
Atoi. This function bears the same name as the one from the C standard library. In Go it invokes ParseInt with a base of 10 and a bit size of 0.

Tip: The Atoi call here is simpler than ParseInt. This gives it an advantage in readability in programs.

Golang program that uses Atoi package main import ( "fmt" "strconv" ) func main() { value := "456" // Use Atoi to parse string. number, _ := strconv.Atoi(value) fmt.Println(number) fmt.Println(number + 1) } Output 456 457
FormatInt. With this method, we convert an int to a string. This is the opposite conversion as ParseInt. With FormatInt, we must pass an int64 value—we cast in this example.
Golang program that uses strconv.FormatInt package main import ( "fmt" "strconv" ) func main() { value := 1055 // Convert int to string with FormatInt. // ... First convert to int64. result := strconv.FormatInt(int64(value), 10) fmt.Println(result) if result == "1055" { fmt.Println(true) } } Output 1055 true
Itoa. This method converts an int into a string. It calls FormatInt with a base of 10. It also accepts a simple int, not an int64, so less casting may be required.

Note: Unlike ParseInt and Atoi, FormatInt and Itoa do not return an error value. They succeed on all possible arguments.

Golang program that uses Itoa, converts int to string package main import ( "fmt" "strconv" ) func main() { value := 700 // Use Itoa on an int. result := strconv.Itoa(value) fmt.Println(result) // The string has 3 characters. fmt.Println(len(result)) } Output 700 3
ParseInt benchmark. Often we need to convert a string into an integer. This benchmark compares ParseInt and Atoi. It converts "1234" into an integer with each method.

Version 1: This version of the code uses the ParseInt method. It repeatedly calls ParseInt in a loop.

Version 2: Here we use the Atoi method, in a similar way as in version 1. We try to determine the time required per method call.

Result: We find that the ParseInt method seems to be faster. It should be preferred when it can be used.

Golang program that benchmarks ParseInt, Atoi package main import ( "fmt" "strconv" "time" ) func main() { t0 := time.Now() original := "1234"; // Version 1: use ParseInt. for i := 0; i < 1000000; i++ { result, _ := strconv.ParseInt(original, 10, 0) if result != 1234 { return } } t1 := time.Now() // Version 2: use Atoi. for i := 0; i < 1000000; i++ { result, _ := strconv.Atoi(original) if result != 1234 { return } } t2 := time.Now() // Results of benchmark. fmt.Println(t1.Sub(t0)) fmt.Println(t2.Sub(t1)) } Output 14.023ms ParseInt 18.5125ms Atoi
A review. We converted strings to integers in Go. With ParseInt and the convenient method Atoi that calls ParseInt with common arguments, we perform this task with ease.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf