no matter the input, commerce.price always returns a round number.
ie. faker.commerce.price(0.1,0.9) will give either 0 or 1 which neither fit the bounds of [0.1-0.9]
I'm not sure if this was the intended functionality but I was trying to use this to get prices that are not always round, ie. $9.24
@HoustonBass maybe you could use faker.finance.amount(1, 100, 2, '$')
@HoustonBass -
It seems that the behavior of Commerce.price is not working as one might expect.
faker.finance.amount(1, 100, 2, '$') does seem to work.
It would be nice if someone could dig into faker.commerce.price and see how it's different from faker.finance.amount, might be we have duplicate API methods now that finance module has been added to library.
@Marak faker.commerce.price doesn't consider decimal digits while generating a random value. Also, there is Math.round used before returning that is faced by @HoustonBass
Additionally, in the example for faker.finance.amount at Wiki, you use faker.commerce.price.
faker.commerce.price(); //returns "625.85"
Yeah, that is what threw me off. can the round be changed so it can have 2
decimals?
On 20 December 2017 at 08:33, Piotr Górecki notifications@github.com
wrote:
Additionally, in the example for faker.finance.amount at Wiki, you use
faker.commerce.price.faker.commerce.price(); //returns "625.85"
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Marak/faker.js/issues/546#issuecomment-353063976, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AE9NsT-_FFBXvoJgn6UDOCDC0d1REHiAks5tCQyggaJpZM4PZ8O3
.
This is as it relates to v3.1.0 ...
The source code for this is literally like this:
/**
* price
*
* @method faker.commerce.price
* @param {number} min
* @param {number} max
* @param {number} dec
* @param {string} symbol
*
* @return {string}
*/
self.price = function(min, max, dec, symbol) {
min = min || 1;
max = max || 1000;
dec = dec === undefined ? 2 : dec;
symbol = symbol || '';
if (min < 0 || max < 0) {
return symbol + 0.00;
}
var randValue = faker.random.number({ max: max, min: min });
return symbol + (Math.round(randValue * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec);
};
The issue is faker.random.number({ max: max, min: min }); -- a couple things there. One... there is a precision you could pass to faker.random.number and that produces kind of the desired result -- like here:
// Produces 35.83 as example.
faker.random.number({min: 1, max: 99, precision: 0.01});
AND this is exactly what the implementation of faker.random.float does.
Code for review here: https://github.com/Marak/faker.js/blob/master/lib/random.js
So as a work you could do this:
// Always produces a whole number or a .00 number
console.log('Bad', faker.commerce.price(1,100,2));
console.log('Bad', faker.finance.amount(1,100,2));
// Works ... but feels dirty.
console.log('Works', faker.random.number({min: 1, max: 99, precision: 0.01}));
Lastly there is a precision problem with the above. Because, well JavaScript. Sometimes you'll ask for X.XX and get.... X.XXXXXXXXXXXXXXXXXX -- so to fix that do this:
faker.random.number({min: 1, max: 99, precision: 0.01}).toFixed(2)
Also, looking at the source code -- looks like this is a faker.random.float method -- but ... it's not exposed. Or is just not in the version I have access to...
Uncaught TypeError: faker.random.float is not a function
Most helpful comment
@HoustonBass -
It seems that the behavior of
Commerce.priceis not working as one might expect.faker.finance.amount(1, 100, 2, '$')does seem to work.It would be nice if someone could dig into
faker.commerce.priceand see how it's different fromfaker.finance.amount, might be we have duplicate API methods now thatfinancemodule has been added to library.