From f8a8cf69dca9aac42fc06c30efcff8690d69c709 Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 6 Aug 2024 16:09:18 -0600 Subject: [PATCH] Et2Number format improvements - If precision is set, always show that many digits after the decimal - Better handling of formatting of initial value --- api/js/etemplate/Et2Textbox/Et2Number.ts | 24 +++++++++---------- .../etemplate/Et2Textbox/Et2NumberReadonly.ts | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/api/js/etemplate/Et2Textbox/Et2Number.ts b/api/js/etemplate/Et2Textbox/Et2Number.ts index 84c8ab0398..78c09413de 100644 --- a/api/js/etemplate/Et2Textbox/Et2Number.ts +++ b/api/js/etemplate/Et2Textbox/Et2Number.ts @@ -298,7 +298,8 @@ export class Et2Number extends Et2Textbox radix: this.decimalSeparator, thousandsSeparator: this.thousandsSeparator, mask: this.mask ?? Number, - lazy: false + lazy: false, + padFractionalZeros: (typeof this.precision !== "undefined") } if(typeof this.precision != "undefined") { @@ -322,18 +323,11 @@ export class Et2Number extends Et2Textbox { // Number mask sometimes gets lost with different decimal characters this._mask.unmaskedValue = ("" + this.value); - - // Fill decimals to precision - if(this.precision && ("" + this.value).includes(".")) - { - this._mask.unmaskedValue = this._mask.unmaskedValue.padEnd(("" + this.value).length + this.precision, "0"); - } } - else if(this.precision && ("" + this.value).includes(".")) + + if(this.value !== "") { - // Fill decimals to precision - let v = formatNumber(this.value, this.decimalSeparator, this.thousandsSeparator); - this._mask.value = v.padEnd(v.length + this.precision, "0"); + this._mask.value = formatNumber(this.value, this.decimalSeparator, this.thousandsSeparator, this.precision); } this._mask.updateValue(); } @@ -407,11 +401,15 @@ export class Et2Number extends Et2Textbox * @param {number} value * @returns {string} */ -export function formatNumber(value : number, decimalSeparator : string = ".", thousandsSeparator : string = "") : string +export function formatNumber(value : number, decimalSeparator : string = ".", thousandsSeparator : string = "", decimalPlaces = undefined) : string { // Split by . because value is a number, so . is decimal separator let parts = ("" + value).split("."); - parts[0] = parts[0].replace(/\B(?