Support roundingMode for InputNumber (#4946)

pull/4992/head
Son Tran 2023-12-22 07:24:57 +07:00 committed by GitHub
parent f16bd6ab2e
commit 078a30720c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -78,6 +78,23 @@ export default {
type: Number,
default: undefined
},
roundingMode: {
type: String,
default: 'halfExpand',
validator(value) {
return [
'ceil',
'floor',
'expand',
'trunc',
'halfCeil',
'halfFloor',
'halfExpand',
'halfTrunc',
'halfEven'
].includes(value);
}
},
min: {
type: Number,
default: null

View File

@ -14,6 +14,17 @@ import { InputTextPassThroughOptions } from '../inputtext';
import { PassThroughOptions } from '../passthrough';
import { ClassComponent, GlobalComponentConstructor, Nullable, PassThrough } from '../ts-helpers';
export declare type RoundingMode =
| 'ceil'
| 'floor'
| 'expand'
| 'trunc'
| 'halfCeil'
| 'halfFloor'
| 'halfExpand'
| 'halfTrunc'
| 'halfEven';
export declare type InputNumberPassThroughOptionType<T = any> = InputNumberPassThroughAttributes | ((options: InputNumberPassThroughMethodOptions<T>) => InputNumberPassThroughAttributes | string) | string | null | undefined;
/**
@ -232,6 +243,11 @@ export interface InputNumberProps {
* the default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by the [ISO 4217 currency code](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=maintenance-agency) list (2 if the list doesn't provide that information).
*/
maxFractionDigits?: number | undefined;
/**
* How decimals should be rounded.
* The default value is `"halfExpand"`, [further information](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingmode).
*/
roundingMode?: RoundingMode;
/**
* Mininum boundary value.
*/

View File

@ -181,7 +181,8 @@ export default {
currencyDisplay: this.currencyDisplay,
useGrouping: this.useGrouping,
minimumFractionDigits: this.minFractionDigits,
maximumFractionDigits: this.maxFractionDigits
maximumFractionDigits: this.maxFractionDigits,
roundingMode: this.roundingMode,
};
},
constructParser() {
@ -225,7 +226,7 @@ export default {
},
getCurrencyExpression() {
if (this.currency) {
const formatter = new Intl.NumberFormat(this.locale, { style: 'currency', currency: this.currency, currencyDisplay: this.currencyDisplay, minimumFractionDigits: 0, maximumFractionDigits: 0 });
const formatter = new Intl.NumberFormat(this.locale, { style: 'currency', currency: this.currency, currencyDisplay: this.currencyDisplay, minimumFractionDigits: 0, maximumFractionDigits: 0, roundingMode: this.roundingMode });
return new RegExp(`[${formatter.format(1).replace(/\s/g, '').replace(this._numeral, '').replace(this._group, '')}]`, 'g');
}
@ -247,7 +248,7 @@ export default {
if (this.suffix) {
this.suffixChar = this.suffix;
} else {
const formatter = new Intl.NumberFormat(this.locale, { style: this.mode, currency: this.currency, currencyDisplay: this.currencyDisplay, minimumFractionDigits: 0, maximumFractionDigits: 0 });
const formatter = new Intl.NumberFormat(this.locale, { style: this.mode, currency: this.currency, currencyDisplay: this.currencyDisplay, minimumFractionDigits: 0, maximumFractionDigits: 0, roundingMode: this.roundingMode });
this.suffixChar = formatter.format(1).split('1')[1];
}