Skip to content

Commit

Permalink
fix: Float functions should return the proper number of decimals (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-lemay authored Oct 9, 2024
1 parent 62c2c94 commit 8930726
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
50 changes: 37 additions & 13 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,54 @@ func (f Faker) RandomNumber(size int) int {

// RandomFloat returns a fake random float number for Faker
func (f Faker) RandomFloat(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return value
value := float64(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float64(f.IntBetween(0, p)) / float64(p)

return value + decimals
}

// Float returns a fake random float number for Faker
func (f Faker) Float(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return value
value := float64(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float64(f.IntBetween(0, p)) / float64(p)

return value + decimals
}

// Float32 returns a fake random float64 number for Faker
// Float32 returns a fake random float32 number for Faker
func (f Faker) Float32(maxDecimals, min, max int) float32 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return float32(value)
value := float32(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float32(f.IntBetween(0, p)) / float32(p)

return value + decimals
}

// Float64 returns a fake random float64 number for Faker
func (f Faker) Float64(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return float64(value)
value := float64(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float64(f.IntBetween(0, p)) / float64(p)

return value + decimals
}

// Int returns a fake Int number for Faker
Expand Down
31 changes: 30 additions & 1 deletion faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,39 @@ func TestUInt64Between(t *testing.T) {

func TestRandomFloat(t *testing.T) {
f := New()
value := f.RandomFloat(1, 1, 100)
value := f.RandomFloat(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
Expect(t, math.Round(value*100)/100, value)
}

func TestFloat(t *testing.T) {
f := New()
value := f.Float(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
Expect(t, math.Round(value*100)/100, value)
}

func TestFloat32(t *testing.T) {
f := New()
value := f.Float32(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float32")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
rounded := float32(math.Round(float64(value*100))/100)
Expect(t, rounded, value)
}

func TestFloat64(t *testing.T) {
f := New()
value := f.Float64(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
Expect(t, math.Round(value*100)/100, value)
}

func TestLetter(t *testing.T) {
Expand Down

0 comments on commit 8930726

Please # to comment.