fix(editor): Add smart decimals directive (#14054)

This commit is contained in:
Csaba Tuncsik
2025-03-20 10:52:51 +01:00
committed by GitHub
parent 996026f358
commit 1a26fc2762
4 changed files with 330 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import { smartDecimal } from './smartDecimal';
describe('smartDecimal', () => {
it('should return the same value if it is an integer', () => {
expect(smartDecimal(42)).toBe(42);
});
it('should return the same value if it has only one decimal place', () => {
expect(smartDecimal(42.5)).toBe(42.5);
});
it('should round to two decimal places by default', () => {
expect(smartDecimal(42.567)).toBe(42.57);
});
it('should round to the specified number of decimal places', () => {
expect(smartDecimal(42.567, 1)).toBe(42.6);
});
it('should handle negative numbers correctly', () => {
expect(smartDecimal(-42.567, 2)).toBe(-42.57);
});
it('should handle zero correctly', () => {
expect(smartDecimal(0)).toBe(0);
});
it('should handle very small numbers correctly', () => {
expect(smartDecimal(0.000567, 5)).toBe(0.00057);
});
it('should round to two decimal if it is smaller than the given one', () => {
expect(smartDecimal(42.56, 3)).toBe(42.56);
});
});

View File

@@ -0,0 +1,13 @@
export const smartDecimal = (value: number, decimals = 2): number => {
// Check if integer
if (Number.isInteger(value)) {
return value;
}
// Check if it has only one decimal place
if (value.toString().split('.')[1].length <= decimals) {
return value;
}
return Number(value.toFixed(decimals));
};