Skip to content

Commit

Permalink
Fixing panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Lefevre committed Mar 17, 2017
1 parent 14c530c commit f0d208b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ func numberRound(number float64, decimals int) string {
multiplier := math.Pow(float64(10), float64(decimals))
multiplied := strconv.FormatFloat(math.Ceil(number*multiplier), 'f', 0, 64)

if len(multiplied)-decimals < 0 {
for i := len(multiplied); i <= decimals; i++ {
multiplied = "0" + multiplied
}
}
str = multiplied[:len(multiplied)-decimals] + "." + multiplied[len(multiplied)-decimals:]
}

Expand Down
9 changes: 6 additions & 3 deletions numbers_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package i18n

import (
. "gopkg.in/check.v1"
)
import . "gopkg.in/check.v1"

func (s *MySuite) TestFormatCurrency(c *C) {

Expand Down Expand Up @@ -474,4 +472,9 @@ func (s *MySuite) TestNumberRound(c *C) {
dec = 3
rounded = numberRound(num, dec)
c.Check(rounded, Equals, "1.235")

num = 0.08627709746360779
dec = 2
rounded = numberRound(num, dec)
c.Check(rounded, Equals, "0.09")
}

0 comments on commit f0d208b

Please sign in to comment.