fix: number 组件使用和 input-number 一样的四舍五入精度策略 Closes #10112 (#10220)

This commit is contained in:
吴多益 2024-05-13 14:55:11 +08:00 committed by GitHub
parent f3bdc837ee
commit cf7521da75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 2 deletions

View File

@ -32,7 +32,7 @@ order: 39
"type": "page",
"body": {
"type": "number",
"value": 123456.34343434343,
"value": 13525646.295,
"precision": 2
}
}

View File

@ -252,6 +252,15 @@ test('Renderer:number with precision and default value', async () => {
expect(container).toMatchSnapshot();
});
test('Renderer:number with precision and round', async () => {
const {input, wrap, container, getByText} = await setup({
precision: 2,
value: 6.295
});
expect(input.value).toBe('6.30');
});
test('Renderer:number with step & precision & displayMode & keyboard', async () => {
const {input, wrap, container, getByText} = await setup({
step: 3,

View File

@ -5,6 +5,13 @@ import {BaseSchema} from '../Schema';
import isNumber from 'lodash/isNumber';
import {getPropValue, Option, PlainObject, normalizeOptions} from 'amis-core';
import getMiniDecimal, {
DecimalClass,
toFixed,
getNumberPrecision,
num2str
} from '@rc-component/mini-decimal';
/**
* Number
* https://aisuda.bce.baidu.com/amis/zh-CN/components/number
@ -111,7 +118,9 @@ export class NumberField extends React.Component<NumberProps> {
viewValue = <span>{value}</span>;
} else {
if (typeof value === 'number' && precision) {
value = value.toFixed(precision);
value = getMiniDecimal(
toFixed(num2str(value), '.', precision)
).toNumber();
}
if (kilobitSeparator) {