Skip to content

Commit

Permalink
Merge pull request #4 from eyeview/round-floats
Browse files Browse the repository at this point in the history
round floats
  • Loading branch information
shaharck authored Feb 21, 2018
2 parents 8cc759b + 215668d commit 91797c3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions statsdaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,13 @@ func parseLine(line []byte) *Packet {
case "c":
value, err = strconv.ParseInt(string(val), 10, 64)
if err != nil {
//try to strip ".0" if being sent that way (overops send it like that :( )
if (strings.HasSuffix(string(val), ".0")) {
value, err = strconv.ParseInt(strings.TrimSuffix(string(val),".0"), 10, 64)
}
//try to round a float
value, err = strconv.ParseFloat(string(val), 64)
if (err != nil) {
log.Printf("ERROR: failed to ParseInt %s - %s", string(val), err)
log.Printf("ERROR: failed to Parse %s - %s", string(val), err)
return nil
} else {
value = round(value.(float64))
}

}
Expand Down Expand Up @@ -601,6 +601,11 @@ func parseLine(line []byte) *Packet {
}
}

func round(val float64) int {
if val < 0 { return int(val-0.5) }
return int(val+0.5)
}

func logParseFail(line []byte) {
if *debug {
log.Printf("ERROR: failed to parse line: %q\n", string(line))
Expand Down

0 comments on commit 91797c3

Please sign in to comment.