How can i convert string into integer without removing the 0 digit prefix from start. Use case like this i have a string like "0093" and i want to convert same as 0093 in integer. I try strconv but problem is this package remove 00 prefix from 0093 after conversion. can any one have better solution for this problem.
s := "0093"
if i, err := strconv.Atoi(s); err == nil {
fmt.Printf("i=%d, type: %T
", i, i)
}
output is 93 but i want exact 0093 in int type.
From the docs for the fmt
package:
Width is specified by an optional decimal number immediately preceding the verb. If absent, the width is whatever is necessary to represent the value.
...
Other flags:
0 pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign
If you combine these two things then you get the code:
fmt.Printf("i=%04d, type: %T
", i, i)