Skip to content

Commit

Permalink
ver. 0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
光弘 committed Mar 26, 2018
1 parent face752 commit caa0ace
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.1.9
* `CHANGED` use `big.js` instead of `parseFloat` to handle large number fixed

# 0.1.8
* `CHANGED` undefined/null/'' return ''
* because they are all invalid value for date.
Expand Down
10 changes: 7 additions & 3 deletions demo/FormatterDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
*/

const Formatter = require('../src');

const log = window.console.log;

log(Formatter.date(new Date(), 'YYYY-MM-DD HH:mm:ss')); // 2015-10-12 14:22:16
log(Formatter.money('6566456.65466545')); // "6 566 456.65466545"
log(Formatter.money('6566456.65466545', ',')); // "6,566,456.65466545"
log(Formatter.money('6566456', ',')); // "6,566,456"
log(Formatter.money('6566456.65466545', ',')); // "6,566,456.65466545"
log(Formatter.money('6566456', ',')); // "6,566,456"
log(Formatter.money('6566456', ',', 4)); // "6,566,456.0000"
log(Formatter.money('6566456', ',', 2)); // "6,566,456.00"
log(Formatter.money('6566456.65466545', ',', 4)); // "6,566,456.6547"
log(Formatter.money('6566456.65466545', ',', 4)); // "6,566,456.6547"
log(Formatter.money('6566456.65466545', ',', 2)); // "6,566,456.65"
log(Formatter.money('1111111111111111111', ',', 2)); // "1,111,111,111,111,111,111.00"
log(Formatter.money('11111111111111111111111111111111111111111111111111', ',', 2)); // "11,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111.00"

module.exports = Formatter;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uxcore-formatter",
"version": "0.1.8",
"version": "0.1.9",
"description": "a formatter library for uxcore",
"repository": "https://github.com/uxcore/uxcore-formatter.git",
"author": "eternalsky",
Expand Down Expand Up @@ -45,6 +45,7 @@
"uxcore-tools": "0.2.x"
},
"dependencies": {
"big.js": "^5.0.3",
"object-assign": "^2.0.0",
"prop-types": "15.x"
},
Expand Down
5 changes: 4 additions & 1 deletion src/Formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* All rights reserved.
*/

import Big from 'big.js';

const Formatter = {};

Formatter.date = (str, pattern) => {
Expand Down Expand Up @@ -61,7 +63,8 @@ Formatter.date = (str, pattern) => {
};

Formatter.money = (str, delimiter = ' ', fixedNum) => {
const actualStr = fixedNum ? parseFloat(str).toFixed(fixedNum).toString() : str;
// const actualStr = fixedNum ? parseFloat(str).toFixed(fixedNum).toString() : str;
const actualStr = fixedNum ? new Big(str).toFixed(fixedNum).toString() : str;
if (actualStr.indexOf('.') !== -1) {
return actualStr.replace(/(\d)(?=(?:\d{3})+(\.))/g, (match, $1) => $1 + delimiter)
.replace(/(\d{3})(?![$|\.|\(|\s])/g, (match, $1) => $1);
Expand Down
5 changes: 5 additions & 0 deletions tests/Formatter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ describe('Formatter.card', () => {
expect(Formatter.card('1234567890123456', ',')).to.be('1234,5678,9012,3456');
expect(Formatter.card('1234567890123456789', ',')).to.be('1234,5678,9012,3456,789');
});

it('works with large number', () => {
expect(Formatter.money('1111111111111111111', ',', 2)).to.be('1,111,111,111,111,111,111.00');
expect(Formatter.money('11111111111111111111111111111111111111111111111111', ',', 2)).to.be('11,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111.00');
});
});

0 comments on commit caa0ace

Please sign in to comment.