잠시만 기다려 주세요

     '월급 200 받는 사람이 국회의원 되는 그날까지... 슈퍼기득권 전부 아웃... 국회의원 봉사직 전환을 꿈꾸며... '
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (19)  |  여러가지 팁 (1095)  |  추천 및 재미 (163)  |  자료실 (28)  |  
시사, 이슈, 칼럼, 평론, 비평 (799)  |  끄적거림 (142)  |  문예 창작 (719)  |  바람 따라 (75)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    golang

golang - strconv.ParseInt strconv.Atoi strconv.FormatInt strconv.Itoa 정수 문자열 변환
이 름 : 바다아이   |   조회수 : 10179         짧은 주소 : https://www.bada-ie.com/su/?271591802826
ParseInt, strconv. A string contains digit characters like "123." We can convert this string to a number, an int, with the ParseInt method.
Other options. With Atoi we have a simpler way to call ParseInt. These methods return two 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 call ParseInt with three arguments—the first is the string.

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.

Based on: Golang 1.7

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.

Result:No big performance differences were found. The methods performed about the same.

So:The best method to use is the one that has the correct behavior on all the data you want to convert.

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))
}

Results

15.637ms    ParseInt
15.625ms    Atoi


출처 : https://www.dotnetperls.com/parseint-go

| |





      1 page / 6 page
번 호 카테고리 제 목 이름 조회수
180 golang golang ... 바다아이 140
179 golang golang , ... 바다아이 2726
178 golang golang , map . 바다아이 2110
177 golang Golang (, , data ) , ... 바다아이 2438
176 golang golang sort ... 바다아이 2808
175 golang golang html.EscapeString html.UnescapeString input value ... 바다아이 2734
174 golang golang go.mod go.sum . GOPATH SRC not module, 1.16 . 바다아이 6260
173 golang go 1.16 ... is not in GOROOT.. GOPATH .... . 바다아이 7413
172 golang , String Formatting 바다아이 8716
171 golang rand.Intn , random, , . 바다아이 8256
170 golang golang ... 바다아이 12007
169 golang golang gopath, goroot .. golang 바다아이 8975
168 golang golang ... Force download file example 바다아이 11004
167 golang golang , , cpu, memory, disk 바다아이 12069
166 golang golang , ... GOOS, GOARCH 바다아이 9716
165 golang golang checkbox ... 바다아이 9595
164 golang golang , , http .... 바다아이 9407
163 golang golang nil , nil , nil ... 바다아이 9490
162 golang 2 golang, go , .... golang .... 바다아이 12736
161 golang golang postgresql, mysql, mariadb ... ` Grave () .. .. 바다아이 10049
160 golang golang postgresql mysql, mariadb scan , null .. 바다아이 10121
159 golang golang , iconv 바다아이 12931
158 golang golang quote escape, unquote 바다아이 10354
157 golang golang , http errorLog , , ... 바다아이 10741
156 golang golang interface , 바다아이 9807
155 golang golang struct .... 바다아이 10532
154 golang golang map map , 바다아이 10009
153 golang golang map .... .... 바다아이 9341
152 golang golang slice copy 바다아이 9530
151 golang golang goto 바다아이 10718
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.28717
to webmaster... gogo sea. gogo sea.