Skip to content

Commit

Permalink
use power instead of for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
feitian124 committed Aug 23, 2021
1 parent dd67250 commit 0f088e6
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,10 @@ func RoundInt(i int64, dec int) int64 {
if dec >= 0 {
return i
}
// The integer part of a fraction
shift := math.Pow10(dec)
intPart := math.Round(float64(i) * shift)
r := int64(intPart)
// the zero part
for i := 1; i <= -dec; i++ {
r *= 10
}
return r

shift := math.Pow10(-dec)
intPart := math.Round(float64(i) / shift)
return int64(intPart) * int64(shift)
}

// Truncate truncates the argument f to dec decimal places.
Expand Down

0 comments on commit 0f088e6

Please sign in to comment.