Skip to content
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
2 changes: 1 addition & 1 deletion src/modules/number/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ export function fakeNumberInt(

const real = distributor(fakerCore.randomizer);
const delta = effectiveMax - effectiveMin + 1; // +1 for inclusive max bounds and even distribution
return Math.floor(real * delta + effectiveMin) * multipleOf;
return (Math.floor(real * delta) + effectiveMin) * multipleOf;
}
34 changes: 34 additions & 0 deletions test/modules/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ describe('number', () => {

expect(results).toEqual([750, 0, 0, 0, 0, 0, 0, 0, 0, 250]);
});

it('should not round past the upper bound with an offset range', () => {
const nearOne = () => MERSENNE_MAX_VALUE;

expect(faker.number.int({ min: 1, max: 2, distributor: nearOne })).toBe(
2
);
expect(
faker.number.int({
min: 10,
max: 101,
multipleOf: 3,
distributor: nearOne,
})
).toBe(99);
expect(
faker.number.int({
min: Number.MAX_SAFE_INTEGER - 1,
max: Number.MAX_SAFE_INTEGER,
distributor: nearOne,
})
).toBe(Number.MAX_SAFE_INTEGER);
});
});

describe('float', () => {
Expand Down Expand Up @@ -363,6 +386,17 @@ describe('number', () => {
expect(results).toEqual([0, 0.5, 1, 1.5]);
});

it('should not round a constrained float past its upper bound', () => {
expect(
faker.number.float({
min: 0.5,
max: 1,
multipleOf: 0.5,
distributor: () => MERSENNE_MAX_VALUE,
})
).toBe(1);
});

it.each(times(100))(
'provides numbers with an exact fractional digits',
() => {
Expand Down
Loading