Golang Learning Notes Golang type conversion finishing go language string, int, int64, float64, complex convert each other

Source: Internet
Author: User

Golang Learning Notes Golang type conversion finishing go language string, int, int64, float64, complex convert each other

#string到int
Int,err:=strconv. Atoi (String)
#string到int64
Int64, err: = StrConv. parseint (String, 10, 64)
#int到string
String:=strconv. Itoa (int)
#int64到string
String:=strconv. Formatint (int64,10)

#int到int64, turn int into a string and then turn to int64, return with the Err parameter need to ignore
S: = StrConv. Itoa (int)
S64,_: = StrConv. parseint (s,10,64)

=======================
Plural, squared, 10 of the 5-time Square
CMPLX. Pow (10,5)
Https://gowalker.org/math/cmplx#Pow
CMPLX. Pow () complex turn into Int64
Take the real part, the float64 type
P: = Real (CMPLX. Pow (10,5))
Float64 turn into Int64
P: = Int64 (Real (CMPLX. Pow (10,5)))
------------------

Float64 turn into Int64
var x float64 = 5.7
var y int = int64 (x)

var value1 complex64 = 3.2 + 12i
Value2: = 3.2 + 12i
Value3: = Complex (3.2, 12)
R = Real (value1)//Get the real part of the plural
i = imag (value1)//Get the imaginary part of the plural
=======================
1. Shaping to a string:
[Plain] View plain copy
var i int = 1
var s string
s = StrConv. Itoa (i) or S = Formatint (Int64 (i), 10)

2. String to Reshape
var s string = "1"
var i int
I, err = StrConv. Atoi (s) or I, err = parseint (s, 10, 0)

3, String to float (32/64)
var s string = 1
var f float32
F, err = parsefloat (S, 32)

Float 64, turn 32 of the above function into 64.

4, shaping to float or float to plastic
Direct conversion directly using float (i) or int (f)

=======================
Golang int is converted to a string method:
var i int = 10
Convert by Itoa method
STR1: = StrConv. Itoa (i)
Convert by sprintf method
STR2: = Fmt. Sprintf ("%d", i)
=======================
(1) int to string
S: = StrConv. Itoa (i)
Equivalent to S: = StrConv. Formatint (Int64 (i), 10)

(2) Int64 turn string
I: = Int64 (123)
S: = StrConv. Formatint (i, 10)
The second parameter is a radix, optional 2~36
Note: For unsigned shaping, you can use Formatuint (i uint64, base int)

(3) string to int
I, err: = StrConv. Atoi (s)

(4) String transfer Int64
I, err: = StrConv. parseint (S, 10, 64)
The second argument is cardinality (2~36), and the third parameter bit size represents the result type of the desired conversion, with a value of 0, 8, 16, 32, and 64, corresponding to int, int8, Int16, Int32, and Int64

(5) Float-related

Float to string:
V: = 3.1415926535
S1: = StrConv. Formatfloat (V, ' E ',-1, +)//float32s2: = StrConv. Formatfloat (V, ' E ',-1, +)//float64

function prototype and parameter meaning specific to view: https://golang.org/pkg/strconv/#FormatFloat

String to float:


S: = "3.1415926535"
V1, err: = StrConv. Parsefloat (V, 32)
V2, Err: = StrConv. Parsefloat (V, 64)

Ps:go languages String, int, int64 convert each other


//string to int
Int,err:=strconv. Atoi (String)
//string to Int64
Int64, err: = StrConv. parseint (String, ten, +)
//int to String
String:=strconv. Itoa (int)
//int64 to String
String:=strconv. Formatint (int64,10)
//string to Float32 (float64)
Float,err: = StrConv. Parsefloat (STRING,32/64)
//float to String
string: = StrConv. Formatfloat (float32, ' E ',-1, +)
String: = StrConv. Formatfloat (float64, ' e ',-1, +)
//' B ' (-ddddp±ddd, binary index)
//' E ' (-d.dddde±dd, decimal index)
//' E ' (-D.DDDDE±DD, Decimal index)
//' F ' (-ddd.dddd, no exponent)
//' G ' (' e ': large exponent, ' F ': other case)
//' G ' (' e ': large exponent, ' F ': other case)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.