diff --git a/address.go b/address.go index f828fe5..8a56d4e 100644 --- a/address.go +++ b/address.go @@ -10,7 +10,7 @@ var ( citySuffix = []string{"town", "ton", "land", "ville", "berg", "burgh", "borough", "bury", "view", "port", "mouth", "stad", "furt", "chester", "mouth", "fort", "haven", "side", "shire"} - buildingNumber = []string{"%####", "%###", "%##"} + buildingNumber = []string{"####", "###", "##"} streetSuffix = []string{ "Alley", "Avenue", diff --git a/faker.go b/faker.go index 641c163..e2492e2 100644 --- a/faker.go +++ b/faker.go @@ -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 diff --git a/faker_test.go b/faker_test.go index 5f7324e..7ec0844 100644 --- a/faker_test.go +++ b/faker_test.go @@ -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) {