number := 111555
How do I get, for instance the first 3 digits (111) and the last 2 digits (55)?
You can convert a number into String and then you can use toCharArray() or split() method to separate the number into digits. String number = String. valueOf(someInt); char[] digits1 = number.
By using simple arithmetic:
func d(num int) (firstThree int, lastTwo int) {
firstThree = num / 1000
lastTwo = num % 100
return
}
This function gives the digit at a specific place:
func digit(num, place int) int {
r := num % int(math.Pow(10, float64(place)))
return r / int(math.Pow(10, float64(place-1)))
}
For example digit(1234567890, 2)
gives 9
and digit(1234567890, 6)
gives 5
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With