diff options
| author | Matt Mayer <[email protected]> | 2023-12-28 01:46:55 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-12-27 18:46:55 +0000 |
| commit | 39c715d916d69e83795932260f1681df3241db91 (patch) | |
| tree | 690e9d3cbbc85e39eb5b181a14530cd829215e03 /src/modules | |
| parent | 24482a30042eec5b553b30d60985e89fd69a8660 (diff) | |
| download | faker-39c715d916d69e83795932260f1681df3241db91.tar.xz faker-39c715d916d69e83795932260f1681df3241db91.zip | |
fix(number): improve float generation for precisions of form 10^-n (#2581)
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/number/index.ts | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index 91d98777..4becb348 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -150,7 +150,12 @@ export class NumberModule extends SimpleModuleBase { throw new FakerError(`Precision should be greater than 0.`); } - const factor = 1 / precision; + const logPrecision = Math.log10(precision); + // Workaround to get integer values for the inverse of all precisions of the form 10^-n + const factor = + precision < 1 && Number.isInteger(logPrecision) + ? 10 ** -logPrecision + : 1 / precision; const int = this.int({ min: min * factor, max: max * factor, |
