Skip to content

Some Int parsing didn't quite work as I would expect #12

@mmdock

Description

@mmdock

Sample:

package main

import (
	"fmt"
	"strconv"
	"github.com/xiam/to"
)

func main() {
	fmt.Println(to.Int64(35.24))//Expects 35; Received: 35
	fmt.Println(to.Int(35.24))//Expect 35; Received: 35
	fmt.Println(to.Int64("35.24")) //Expect 35; Received: 0
	fmt.Println(to.Int("35.24")) //Expect 35; Received: 0
	

        //Potential fix: use `strconv.ParseFloat` instead of `ParseInt`
	i, _ := strconv.ParseFloat("35.24", 64)
	fmt.Println(to.Int(i)) 
}

Context: Some client behavior in my application was truncating the floating value and converting to ints before sending it to my server. Was trying to make it such that the client now didn't bother to truncate and directly would send the float, as I was not expecting this library's code to treat strings differently than it did regular floats.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions