@@ -490,6 +490,7 @@ class FloatField(Field):
490490 min_val : float | None = None
491491 max_val : float | None = None
492492 allowed : list [float ] | None = field (default = None )
493+ precision : int | None = None
493494
494495 # Override dtype with default
495496 dtype : str = "Float64"
@@ -517,6 +518,10 @@ def _validate(self) -> None:
517518 if len (self .allowed ) == 0 :
518519 raise ValueError ("allowed list cannot be empty" )
519520
521+ # Validate precision
522+ if self .precision is not None and self .precision < 0 :
523+ raise ValueError (f"precision ({ self .precision } ) must be a non-negative integer" )
524+
520525 def has_allowed_values (self ) -> bool :
521526 """Check if this field has a set of allowed values."""
522527 return self .allowed is not None
@@ -526,6 +531,7 @@ def float_field(
526531 min_val : float | None = None ,
527532 max_val : float | None = None ,
528533 allowed : list [float ] | None = None ,
534+ precision : int | None = None ,
529535 nullable : bool = False ,
530536 null_probability : float = 0.0 ,
531537 unique : bool = False ,
@@ -555,6 +561,9 @@ def float_field(
555561 allowed
556562 List of allowed values (categorical constraint). When provided, values are sampled from
557563 this list. Cannot be combined with `min_val=`/`max_val=`.
564+ precision
565+ Number of decimal places to round generated values to. Default is `None` (no rounding).
566+ Must be a non-negative integer. Has no effect when `allowed=` or `generator=` is used.
558567 nullable
559568 Whether the column can contain null values. Default is `False`.
560569 null_probability
@@ -578,8 +587,8 @@ def float_field(
578587 ------
579588 ValueError
580589 If `min_val` is greater than `max_val`, if `allowed` is an empty list, if
581- `null_probability` is not between `0.0` and `1.0`, or if `dtype ` is not a valid
582- float type.
590+ `null_probability` is not between `0.0` and `1.0`, if `precision ` is negative,
591+ or if `dtype` is not a valid float type.
583592
584593 Examples
585594 --------
@@ -620,7 +629,20 @@ def float_field(
620629 calibration=pb.float_field(min_val=0.9, max_val=1.1),
621630 )
622631
623- pb.preview(pb.generate_dataset(schema, n=30, seed=7))
632+ pb.preview(pb.generate_dataset(schema, n=30, seed=23))
633+ ```
634+
635+ Use `precision=` to round generated values to a fixed number of decimal places. This is useful
636+ for prices, scores, or any measurement where full floating-point precision is unwanted:
637+
638+ ```{python}
639+ schema = pb.Schema(
640+ price=pb.float_field(min_val=1.0, max_val=200.0, precision=2),
641+ score=pb.float_field(min_val=0.0, max_val=100.0, precision=1),
642+ probability=pb.float_field(min_val=0.0, max_val=1.0, precision=4),
643+ )
644+
645+ pb.preview(pb.generate_dataset(schema, n=20, seed=23))
624646 ```
625647
626648 Setting `dtype="Float32"` gives reduced precision, and a custom `generator=` provides
@@ -636,13 +658,14 @@ def float_field(
636658 log_value=pb.float_field(generator=lambda: math.log(rng.uniform(1, 1000))),
637659 )
638660
639- pb.preview(pb.generate_dataset(schema, n=20, seed=99 ))
661+ pb.preview(pb.generate_dataset(schema, n=20, seed=23 ))
640662 ```
641663 """
642664 return FloatField (
643665 min_val = min_val ,
644666 max_val = max_val ,
645667 allowed = allowed ,
668+ precision = precision ,
646669 nullable = nullable ,
647670 null_probability = null_probability ,
648671 unique = unique ,
0 commit comments