Golang Truncating Float32 From Scanner Input

274 views Asked by At

I am attempting to create a program where you can input a float32 into a scanner, and have the output be a truncation of the float, removing all digits from the right of the decimal place.

Example:
Input: 6.7777
Output: 6

The problem I'm having with my code is the ParseFloat conversion is rounding up my number instead of truncating the number.

Example:
Input: 6.7777
Output: 7

-This Is My Code-

I was asked to remove my code. My apologies for any inconvenience. 

Any golang savants out there that can help?
I'm starting to think i may be using the wrong functions for my program requirements, can i make this code work? Or do i need to take another approach?
Thank you for any time that can be spared to assist.

1

There are 1 answers

0
Prabhakar Reddy On
      1. Need correction in fmt.Printf() statement use %d and cast the 
         input to int(input)       
        
     package main
    
     import (
        "bufio"
        "fmt"
        "os"
        "strconv"
    )
    
    func main() {
        scanner := bufio.NewScanner(os.Stdin)
        fmt.Printf("Enter Floating Point Number: ")
        scanner.Scan()
        input, _ := strconv.ParseFloat(scanner.Text(), 64)
        fmt.Printf("Floating Point Number Truncated: %d", int(input))
    
    }
    Output:
    -------
    Enter Floating Point Number: 6.777
    Floating Point Number Truncated: 6