Sergey Onishchenko
Sergey Onishchenko

Reputation: 7851

Extract digits at a certain position in a number

number := 111555

How do I get, for instance the first 3 digits (111) and the last 2 digits (55)?

Upvotes: 7

Views: 19708

Answers (7)

Kaveh Shahbazian
Kaveh Shahbazian

Reputation: 13513

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) (second number from the right side) gives 9 and digit(1234567890, 6) gives 5 (sixth number from the right side).

Upvotes: 6

wasmup
wasmup

Reputation: 16223

Try this:

number := 111555
fmt.Println(number % 100) // last 2 digits (55)

// first 3 digits (111)
for ; number > 999; number /= 10 {
}
fmt.Println(number) // 111

Try this:

number := 111555
slice := strconv.Itoa(number)    
fmt.Println(slice[:3]) // first 3 digits (111)    
fmt.Println(slice[len(slice)-2:]) // and the last 2 digits (55)

The first 3 digits (111):

strconv.Itoa(number)[:3]

and the last 2 digits (55):

number % 100

or:

slice := strconv.Itoa(number)
slice[len(slice)-2:]

try this:

    number := 111555
    // and the last 2 digits (55)
    l2d := number % 100
    fmt.Println(l2d)

    // first 3 digits (111)
    f3d := strconv.Itoa(number)[:3]
    // i, e := strconv.Atoi(f3d)
    fmt.Println(f3d)

Another way, try this:

package main

import (
    "fmt"
    "math"
)

func main() {
    number := 111555
    // last 2 digits (55)
    l2d := number % 100
    fmt.Println(l2d)

    // first 3 digits (111)
    remainingDigits := numberOfDigits(number) - 3
    f3d := number / int(math.Pow10(remainingDigits))
    fmt.Println(f3d)
}
func numberOfDigits(n int) (digits int) {
    for ; n != 0; n /= 10 {
        digits++
    }
    return
}

another way:

package main

import (
    "fmt"
)

func main() {
    number := 111555
    // and the last 2 digits (55)
    l2d := number % 100
    fmt.Println(l2d)
    // first 3 digits (111)
    fmt.Println(leftDigits(number, 3))

}

func leftDigits(number, n int) int {
    digits := make([]byte, 20)
    i := -1
    for number != 0 {
        i++
        digits[i] = byte(number % 10)
        number /= 10
    }
    r := 0
    for ; n != 0; n-- {
        r = r * 10
        r += int(digits[i])
        i--
    }
    return r
}

output:

55
111

Upvotes: 9

peterSO
peterSO

Reputation: 166569

For example,

package main

import (
    "fmt"
    "strconv"
)

func strDigits(n, digits int) (string, int) {
    s := strconv.Itoa(n)
    if s[0] == '-' {
        s = s[1:]
    }
    if digits > len(s) {
        digits = len(s)
    }
    if digits < 0 {
        digits = 0
    }
    return s, digits
}

func firstDigits(n, digits int) string {
    s, d := strDigits(n, digits)
    return s[:d]
}

func lastDigits(n, digits int) string {
    s, d := strDigits(n, digits)
    return s[len(s)-d:]
}

func main() {
    number := 111555
    fmt.Println(firstDigits(number, 3))
    fmt.Println(lastDigits(number, 2))
}

Playground: https://play.golang.org/p/oYwZ5HFndt

Output:

111
55

Upvotes: 1

Sergey Onishchenko
Sergey Onishchenko

Reputation: 7851

str := strconv.Itoa(111555)
fmt.Println(str[len(str)-2:]) //trailing 2 digits 
fmt.Println(str[:3]) //first 3 digits

Upvotes: 1

augustzf
augustzf

Reputation: 2415

By using simple arithmetic:

func d(num int) (firstThree int, lastTwo int) {
    firstThree = num / 1000
    lastTwo = num % 100
    return
}

Upvotes: 6

littlepoint
littlepoint

Reputation: 124

If the number is not too large, there is a simple way.

package main
import (
    "fmt"
    "log"
    "strconv"
)
func main() {
    num := 111555
    s := strconv.Itoa(num)
    pos := 3 // first 3 digits, others
    part1 := s[:pos]
    part2 := s[pos:]
    num1, err := strconv.Atoi(part1)
    if err != nil {
        log.Fatal(err)
    }
    num2, err := strconv.Atoi(part2)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(num1, num2)
}

Upvotes: 1

CallMeLoki
CallMeLoki

Reputation: 1361

you cant turn int into slice of digits in golang

the trick is u have to turn it into string, do the composition, then fallback to int seperatly

https://play.golang.org/p/eZn1onG7T6

Upvotes: 1

Related Questions