@@ -97,18 +97,26 @@ export class NumberModule extends SimpleModuleBase {
9797 * @param options.multipleOf The generated number will be a multiple of this property.
9898 * This property can be used to limit the result to a specific number of decimal digits.
9999 * For example `0.01` will round to 2 decimal points.
100- * If multipleOf is passed, the upper bound is inclusive.
100+ * If `multipleOf` is passed, the upper bound is inclusive.
101+ * This option is incompatible with the `fractionDigits` option.
102+ * @param options.fractionDigits The maximum number of digits to appear after the decimal point.
103+ * This option is incompatible with the `multipleOf` option.
101104 *
102105 * @throws When `min` is greater than `max`.
103106 * @throws When `precision` is negative.
107+ * @throws When `multipleOf` is negative.
108+ * @throws When `fractionDigits` is negative.
109+ * @throws When `fractionDigits` and `multipleOf` is passed in the same options object.
104110 *
105111 * @example
106112 * faker.number.float() // 0.5688541042618454
107113 * faker.number.float(3) // 2.367973240558058
108114 * faker.number.float({ min: -1000000 }) //-780678.849672846
109115 * faker.number.float({ max: 100 }) // 17.3687307164073
110116 * faker.number.float({ multipleOf: 0.25 }) // 3.75
111- * faker.number.float({ min: 10, max: 100, multipleOf: 0.001 }) // 35.415
117+ * faker.number.float({ fractionDigits: 1 }) // 0.9
118+ * faker.number.float({ min: 10, max: 100, multipleOf: 0.02 }) // 35.42
119+ * faker.number.float({ min: 10, max: 100, fractionDigits: 3 }) // 65.716
112120 *
113121 * @since 8.0.0
114122 */
@@ -129,6 +137,10 @@ export class NumberModule extends SimpleModuleBase {
129137 */
130138 max ?: number ;
131139 /**
140+ * The number of digits to appear after the decimal point.
141+ */
142+ fractionDigits ?: number ;
143+ /*
132144 * Precision of the generated number.
133145 *
134146 * @deprecated Use `multipleOf` instead.
@@ -147,10 +159,17 @@ export class NumberModule extends SimpleModuleBase {
147159 } ;
148160 }
149161
150- // eslint-disable-next-line deprecation/deprecation
151- const { min = 0 , max = 1 , precision, multipleOf = precision } = options ;
162+ const {
163+ min = 0 ,
164+ max = 1 ,
165+ fractionDigits,
166+ precision,
167+ multipleOf : originalMultipleOf = precision ,
168+ multipleOf = precision ??
169+ ( fractionDigits == null ? undefined : 10 ** - fractionDigits ) ,
170+ } = options ;
152171
153- if ( precision !== undefined ) {
172+ if ( precision != null ) {
154173 deprecated ( {
155174 deprecated : 'faker.number.float({ precision })' ,
156175 proposed : 'faker.number.float({ multipleOf })' ,
@@ -167,7 +186,25 @@ export class NumberModule extends SimpleModuleBase {
167186 throw new FakerError ( `Max ${ max } should be greater than min ${ min } .` ) ;
168187 }
169188
170- if ( multipleOf !== undefined ) {
189+ if ( fractionDigits != null ) {
190+ if ( originalMultipleOf != null ) {
191+ throw new FakerError (
192+ 'multipleOf and fractionDigits cannot be set at the same time.'
193+ ) ;
194+ }
195+
196+ if ( ! Number . isInteger ( fractionDigits ) ) {
197+ throw new FakerError ( 'fractionDigits should be an integer.' ) ;
198+ }
199+
200+ if ( fractionDigits < 0 ) {
201+ throw new FakerError (
202+ 'fractionDigits should be greater than or equal to 0.'
203+ ) ;
204+ }
205+ }
206+
207+ if ( multipleOf != null ) {
171208 if ( multipleOf <= 0 ) {
172209 // TODO @xDivisionByZerox : Clean up in v9.0
173210 throw new FakerError ( `multipleOf/precision should be greater than 0.` ) ;
0 commit comments