Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Float functions should return the proper number of decimals #178

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading