This commit is contained in:
@@ -94,6 +94,15 @@ describe('valueFormats', () => {
|
|||||||
|
|
||||||
expect(toFixed(100.4)).toBe('100');
|
expect(toFixed(100.4)).toBe('100');
|
||||||
expect(toFixed(100.5)).toBe('101');
|
expect(toFixed(100.5)).toBe('101');
|
||||||
|
expect(toFixed(27.4)).toBe('27.4');
|
||||||
|
expect(toFixed(27.5)).toBe('27.5');
|
||||||
|
|
||||||
|
expect(toFixed(-100)).toBe('-100');
|
||||||
|
|
||||||
|
expect(toFixed(-100.5)).toBe('-100');
|
||||||
|
expect(toFixed(-100.6)).toBe('-101');
|
||||||
|
expect(toFixed(-27.5)).toBe('-27.5');
|
||||||
|
expect(toFixed(-27.6)).toBe('-27.6');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('toFixed should handle number correctly if decimal is not null', () => {
|
it('toFixed should handle number correctly if decimal is not null', () => {
|
||||||
|
|||||||
@@ -80,10 +80,11 @@ export function toFixed(value: number, decimals?: DecimalCount): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getDecimalsForValue(value: number): number {
|
function getDecimalsForValue(value: number): number {
|
||||||
const log10 = Math.floor(Math.log(Math.abs(value)) / Math.LN10);
|
const absValue = Math.abs(value);
|
||||||
|
const log10 = Math.floor(Math.log(absValue) / Math.LN10);
|
||||||
let dec = -log10 + 1;
|
let dec = -log10 + 1;
|
||||||
const magn = Math.pow(10, -dec);
|
const magn = Math.pow(10, -dec);
|
||||||
const norm = value / magn; // norm is between 1.0 and 10.0
|
const norm = absValue / magn; // norm is between 1.0 and 10.0
|
||||||
|
|
||||||
// special case for 2.5, requires an extra decimal
|
// special case for 2.5, requires an extra decimal
|
||||||
if (norm > 2.25) {
|
if (norm > 2.25) {
|
||||||
|
|||||||
Reference in New Issue
Block a user