子字符串和舍入

package main

import (
    "fmt"
    "bufio"
    "os"
    "strconv"
)

func main() {
    fmt.Print("loaded
")
    var xInp = bufio.NewScanner(os.Stdin) 
    var yInp = bufio.NewScanner(os.Stdin)

    fmt.Print("insert y value: ")
    yInp.Scan()
    fmt.Print("Insert x value: ")
    xInp.Scan()

    q, err := strconv.Atoi(yInp.Text())
    w, err := strconv.Atoi(xInp.Text())

    var slope = q/w
    fmt.Print(slope)
    fmt.Print(err)
}

I am trying to make this code have a substring. When I type in as y, 190. And x as 13. The answer, the program claims is 14. But that is not true. It is a infinite decimal. Obviously I do not want to show the whole decimal. But I do want to show 4 decimal places. for example 190/13 = 14.6153. It is also fine if you know how to round the decimal. That probably would be better. But both are fine.

As far as I understood you want just two divide two numbers and output the result (it has nothing to do substrings).

The problem that you divide integer by integer and therefore gets back an integer. The main changes that I have done in your program are:

var slope = float64(q)/float64(w)  // converted both ints to floats
fmt.Printf("%.4f
", slope)   // printed float to 4 decimal points

Basically here it is:

package main

import (
    "fmt"
    "bufio"
    "os"
    "strconv"
)

func main() {
    var xInp = bufio.NewScanner(os.Stdin)
    var yInp = bufio.NewScanner(os.Stdin)
    fmt.Print("insert y value: ")
    yInp.Scan()
    fmt.Print("Insert x value: ")
    xInp.Scan()
    q, err := strconv.Atoi(yInp.Text())
    w, err := strconv.Atoi(xInp.Text())
    var slope = float64(q)/float64(w)
    fmt.Printf("%.4f
", slope)
    fmt.Println(err)
}