aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/finance.js15
-rw-r--r--test/finance.unit.js20
2 files changed, 31 insertions, 4 deletions
diff --git a/lib/finance.js b/lib/finance.js
index f9afa3a1..bbe2b17f 100644
--- a/lib/finance.js
+++ b/lib/finance.js
@@ -103,16 +103,23 @@ var Finance = function (faker) {
*
* @return {string}
*/
- self.amount = function (min, max, dec, symbol) {
+ self.amount = function (min, max, dec, symbol, autoFormat) {
min = min || 0;
max = max || 1000;
dec = dec === undefined ? 2 : dec;
symbol = symbol || '';
- var randValue = faker.random.number({ max: max, min: min, precision: Math.pow(10, -dec) });
- var stringNumber = symbol + randValue.toFixed(dec);
+ const randValue = faker.random.number({ max: max, min: min, precision: Math.pow(10, -dec) });
- return symbol + randValue.toFixed(dec);
+ let formattedString;
+ if(autoFormat) {
+ formattedString = randValue.toLocaleString(undefined, {minimumFractionDigits: dec});
+ }
+ else {
+ formattedString = randValue.toFixed(dec);
+ }
+
+ return symbol + formattedString;
};
/**
diff --git a/test/finance.unit.js b/test/finance.unit.js
index ae9db61c..ea474f59 100644
--- a/test/finance.unit.js
+++ b/test/finance.unit.js
@@ -223,6 +223,26 @@ describe('finance.js', function () {
assert.strictEqual(typeOfAmount , "string", "the amount type should be number");
});
+ [false, undefined].forEach(function (autoFormat){
+ it(`should return unformatted if autoformat is ${autoFormat}`, function() {
+
+ const number = 6000;
+ const amount = faker.finance.amount(number, number, 0, undefined, autoFormat);
+
+ assert.strictEqual(amount, number.toString());
+ });
+ });
+
+ it("should return the number formatted on the current locale", function() {
+
+ const number = 6000, decimalPlaces = 2;
+ const expected = number.toLocaleString(undefined, {minimumFractionDigits: decimalPlaces});
+
+ const amount = faker.finance.amount(number, number, decimalPlaces, undefined, true);
+
+ assert.strictEqual(amount, expected);
+ });
+
});
describe('transactionType()', function () {