Skip to content

Commit

Permalink
Merge pull request #33 from cibernox/fix-negative-zero
Browse files Browse the repository at this point in the history
Fix negative zero issues
  • Loading branch information
RobbieTheWagner authored Dec 1, 2017
2 parents ec3e5df + b96df94 commit e5a7362
Show file tree
Hide file tree
Showing 6 changed files with 1,410 additions and 1,006 deletions.
12 changes: 9 additions & 3 deletions addon/format-money.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import unformat from "./unformat";
import formatNumber from "./format-number";
import { currency } from "./settings";
import { defaults, checkPrecision, isObject, checkCurrencyFormat } from "./utils";
import toFixed from "./to-fixed";

/**
* Format a number into currency
Expand Down Expand Up @@ -48,7 +49,7 @@ function formatMoney(number, symbol, precision, thousand, decimal, format) {
number = unformat(number);

// Build options object from second param (if object) or all params, extending defaults:
var opts = defaults(
const opts = defaults(
(isObject(symbol) ? symbol : {
symbol : symbol,
precision : precision,
Expand All @@ -60,10 +61,15 @@ function formatMoney(number, symbol, precision, thousand, decimal, format) {
);

// Check format (returns object with pos, neg and zero):
var formats = checkCurrencyFormat(opts.format);
const formats = checkCurrencyFormat(opts.format);

// Clean up precision
const usePrecision = checkPrecision(opts.precision);

// fixedNumber's value is not really used, just used to determine negative or not
const fixedNumber = toFixed(number || 0, usePrecision);
// Choose which format to use for this value:
var useFormat = number > 0 ? formats.pos : number < 0 ? formats.neg : formats.zero;
const useFormat = fixedNumber > 0 ? formats.pos : fixedNumber < 0 ? formats.neg : formats.zero;

// Return with currency symbol added:
return useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(number), checkPrecision(opts.precision), opts.thousand, opts.decimal));
Expand Down
13 changes: 7 additions & 6 deletions addon/format-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defaults, checkPrecision, isObject } from "./utils";
import unformat from "./unformat";
import toFixed from "./to-fixed";

var numberSettings = number;
const numberSettings = number;

/**
* Format a number, with comma-separated thousands and custom precision/decimal places
Expand Down Expand Up @@ -37,7 +37,7 @@ function formatNumber(number, precision, thousand, decimal) {
number = unformat(number);

// Build options object from second param (if object) or all params, extending defaults:
var opts = defaults(
const opts = defaults(
(isObject(precision) ? precision : {
precision : precision,
thousand : thousand,
Expand All @@ -47,12 +47,13 @@ function formatNumber(number, precision, thousand, decimal) {
);

// Clean up precision
var usePrecision = checkPrecision(opts.precision);
const usePrecision = checkPrecision(opts.precision);

// Do some calc:
var negative = number < 0 ? "-" : "";
var base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + "";
var mod = base.length > 3 ? base.length % 3 : 0;
const fixedNumber = toFixed(number || 0, usePrecision);
const negative = fixedNumber < 0 ? "-" : "";
const base = String(parseInt(Math.abs(fixedNumber), 10));
const mod = base.length > 3 ? base.length % 3 : 0;

// Format the number:
return negative + (mod ? base.substr(0, mod) + opts.thousand : "") + base.substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + opts.thousand) + (usePrecision ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] : "");
Expand Down
Loading

0 comments on commit e5a7362

Please # to comment.