@mathigon/fermat
Advanced tools
Comparing version 1.1.2 to 1.1.4
@@ -103,8 +103,10 @@ "use strict"; | ||
return `${n}`; | ||
const d = `${Math.abs(Math.floor(n))}`.length; | ||
const m = n < 0 ? 1 : 0; | ||
if (d <= places - m) | ||
return `${round(n, places - d - m - 1)}`; | ||
const digits2 = `${Math.abs(Math.floor(n))}`.length; | ||
const chars = digits2 + (n < 0 ? 1 : 0); | ||
if (chars <= places) | ||
return `${round(n, places - chars)}`; | ||
const x = Math.floor(Math.log10(Math.abs(n)) / 3); | ||
return round(n / Math.pow(10, 3 * x), places - (d % 3 || 3) - m - 1) + POWER_SUFFIX[x]; | ||
const suffix = POWER_SUFFIX[x]; | ||
const decimalPlaces = places - (digits2 % 3 || 3) - (suffix ? 1 : 0) - (n < 0 ? 1 : 0); | ||
return round(n / Math.pow(10, 3 * x), decimalPlaces) + suffix; | ||
} | ||
@@ -111,0 +113,0 @@ function numberFormat(n, places = 0, separators = true) { |
@@ -37,8 +37,10 @@ var __defProp = Object.defineProperty; | ||
return `${n}`; | ||
const d = `${Math.abs(Math.floor(n))}`.length; | ||
const m = n < 0 ? 1 : 0; | ||
if (d <= places - m) | ||
return `${round(n, places - d - m - 1)}`; | ||
const digits2 = `${Math.abs(Math.floor(n))}`.length; | ||
const chars = digits2 + (n < 0 ? 1 : 0); | ||
if (chars <= places) | ||
return `${round(n, places - chars)}`; | ||
const x = Math.floor(Math.log10(Math.abs(n)) / 3); | ||
return round(n / Math.pow(10, 3 * x), places - (d % 3 || 3) - m - 1) + POWER_SUFFIX[x]; | ||
const suffix = POWER_SUFFIX[x]; | ||
const decimalPlaces = places - (digits2 % 3 || 3) - (suffix ? 1 : 0) - (n < 0 ? 1 : 0); | ||
return round(n / Math.pow(10, 3 * x), decimalPlaces) + suffix; | ||
} | ||
@@ -45,0 +47,0 @@ function numberFormat(n, places = 0, separators = true) { |
@@ -1,2 +0,2 @@ | ||
declare type Matrix = number[][]; | ||
type Matrix = number[][]; | ||
/** Fills a matrix of size x, y with a given value. */ | ||
@@ -3,0 +3,0 @@ export declare function fill(value: number, x: number, y: number): number[][]; |
@@ -1,2 +0,2 @@ | ||
declare type Coordinate = [number, number]; | ||
type Coordinate = [number, number]; | ||
/** | ||
@@ -3,0 +3,0 @@ * Finds a linear regression that best approximates a set of data. The result |
@@ -1,2 +0,2 @@ | ||
declare type Suffix = '%' | 'π' | undefined; | ||
type Suffix = '%' | 'π' | undefined; | ||
/** Extended Number class. */ | ||
@@ -3,0 +3,0 @@ export declare class XNumber { |
{ | ||
"name": "@mathigon/fermat", | ||
"version": "1.1.2", | ||
"version": "1.1.4", | ||
"license": "MIT", | ||
@@ -38,16 +38,16 @@ "homepage": "https://mathigon.io/fermat", | ||
"dependencies": { | ||
"@mathigon/core": "1.1.2" | ||
"@mathigon/core": "1.1.4" | ||
}, | ||
"devDependencies": { | ||
"@types/tape": "4.13.2", | ||
"@typescript-eslint/eslint-plugin": "5.36.1", | ||
"@typescript-eslint/parser": "5.36.1", | ||
"esbuild": "0.15.6", | ||
"eslint": "8.22.0", | ||
"@typescript-eslint/eslint-plugin": "5.45.0", | ||
"@typescript-eslint/parser": "5.45.0", | ||
"esbuild": "0.15.16", | ||
"eslint": "8.28.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"tape": "5.6.0", | ||
"tape": "5.6.1", | ||
"ts-node": "10.9.1", | ||
"tslib": "2.4.0", | ||
"typescript": "4.8.2" | ||
"tslib": "2.4.1", | ||
"typescript": "4.9.3" | ||
} | ||
} |
@@ -54,10 +54,11 @@ // ============================================================================ | ||
// Trim short numbers to the appropriate number of decimal places. | ||
const d = (`${Math.abs(Math.floor(n))}`).length; | ||
const m = n < 0 ? 1 : 0; | ||
if (d <= places - m) return `${round(n, places - d - m - 1)}`; | ||
const digits = (`${Math.abs(Math.floor(n))}`).length; | ||
const chars = digits + (n < 0 ? 1 : 0); | ||
if (chars <= places) return `${round(n, places - chars)}`; | ||
// Append a power suffix to longer numbers. | ||
const x = Math.floor(Math.log10(Math.abs(n)) / 3); | ||
return (round(n / Math.pow(10, 3 * x), places - ((d % 3) || 3) - m - 1)) + | ||
POWER_SUFFIX[x]; | ||
const suffix = POWER_SUFFIX[x]; | ||
const decimalPlaces = places - ((digits % 3) || 3) - (suffix ? 1 : 0) - (n < 0 ? 1 : 0); | ||
return round(n / Math.pow(10, 3 * x), decimalPlaces) + suffix; | ||
} | ||
@@ -64,0 +65,0 @@ |
@@ -8,5 +8,43 @@ // ============================================================================= | ||
import tape from 'tape'; | ||
import {parseNumber, toWord} from '../src'; | ||
import {numberFormat, parseNumber, toWord} from '../src'; | ||
tape('numberFormat', (test) => { | ||
test.equal(numberFormat(1234, 5), '1,234', ':: numberFormat(1234, 5)'); | ||
test.equal(numberFormat(1234, 4), '1,234', ':: numberFormat(1234, 4)'); | ||
test.equal(numberFormat(1234, 3), '1.2k', ':: numberFormat(1234, 3)'); | ||
test.equal(numberFormat(1000, 3), '1k', ':: numberFormat(1000, 3)'); | ||
test.equal(numberFormat(-1234, 6), '–1,234', ':: numberFormat(-1234, 6)'); | ||
test.equal(numberFormat(-1234, 5), '–1,234', ':: numberFormat(-1234, 5)'); | ||
test.equal(numberFormat(-1234, 4), '–1.2k', ':: numberFormat(-1234, 4)'); | ||
test.equal(numberFormat(-1000, 4), '–1k', ':: numberFormat(-1000, 4)'); | ||
test.equal(numberFormat(10001, 5), '10,001', ':: numberFormat(10001, 5)'); | ||
test.equal(numberFormat(10001, 4), '10k', ':: numberFormat(10001, 4)'); | ||
test.equal(numberFormat(-10001, 6), '–10,001', ':: numberFormat(-10001, 6)'); | ||
test.equal(numberFormat(-10001, 5), '–10k', ':: numberFormat(-10001, 5)'); | ||
test.equal(numberFormat(100001, 6), '100,001', ':: numberFormat(100001, 6)'); | ||
test.equal(numberFormat(100001, 5), '100k', ':: numberFormat(100001, 5)'); | ||
test.equal(numberFormat(-100001, 7), '–100,001', ':: numberFormat(-100001, 7)'); | ||
test.equal(numberFormat(-100001, 6), '–100k', ':: numberFormat(-100001, 6)'); | ||
test.equal(numberFormat(1000001, 7), '1,000,001', ':: numberFormat(1000001, 7)'); | ||
test.equal(numberFormat(1000001, 6), '1m', ':: numberFormat(1000001, 6)'); | ||
test.equal(numberFormat(-1000001, 8), '–1,000,001', ':: numberFormat(-1000001, 8)'); | ||
test.equal(numberFormat(-1000001, 7), '–1m', ':: numberFormat(-1000001, 7)'); | ||
test.equal(numberFormat(0.1, 2), '0.1', ':: numberFormat(0.1, 2)'); | ||
test.equal(numberFormat(0.1, 1), '0', ':: numberFormat(0.1, 1)'); | ||
test.equal(numberFormat(-0.1, 3), '–0.1', ':: numberFormat(-0.1, 3)'); | ||
test.equal(numberFormat(-0.1, 2), '0', ':: numberFormat(-0.1, 2)'); | ||
test.equal(numberFormat(0.01, 3), '0.01', ':: numberFormat(0.01, 3)'); | ||
test.equal(numberFormat(0.01, 2), '0', ':: numberFormat(0.01, 2)'); | ||
test.equal(numberFormat(-0.01, 4), '–0.01', ':: numberFormat(-0.01, 4)'); | ||
test.equal(numberFormat(-0.01, 3), '0', ':: numberFormat(-0.01, 3)'); | ||
test.equal(numberFormat(0.001, 4), '0.001', ':: numberFormat(0.001, 4)'); | ||
test.equal(numberFormat(0.001, 3), '0', ':: numberFormat(0.001, 3)'); | ||
test.equal(numberFormat(-0.001, 5), '–0.001', ':: numberFormat(-0.001, 5)'); | ||
test.equal(numberFormat(-0.001, 4), '0', ':: numberFormat(-0.001, 4)'); | ||
test.end(); | ||
}); | ||
tape('parseNumber', (test) => { | ||
@@ -13,0 +51,0 @@ test.equal(parseNumber('1234'), 1234); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
292783
4290
+ Added@mathigon/core@1.1.4(transitive)
- Removed@mathigon/core@1.1.2(transitive)
Updated@mathigon/core@1.1.4