fix: fix accuracy of percent (#685)
* fix: fix accuracy of percent * fix: fix accuracy of percent * fix: test unit modify * fix: export number Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
parent
5e1b1170a8
commit
92cd76ce48
@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
|
||||||
import { connect, mapReadPretty } from '@formily/react';
|
import { connect, mapReadPretty } from '@formily/react';
|
||||||
import { InputNumber as AntdNumber } from 'antd';
|
import { InputNumber as AntdNumber } from 'antd';
|
||||||
|
import React from 'react';
|
||||||
import { ReadPretty } from './ReadPretty';
|
import { ReadPretty } from './ReadPretty';
|
||||||
|
|
||||||
export const InputNumber = connect((props) => {
|
export const InputNumber = connect((props) => {
|
||||||
|
@ -1,19 +1,9 @@
|
|||||||
import { isValid } from '@formily/shared';
|
import { isValid } from '@formily/shared';
|
||||||
|
import { toFixedByStep } from '@nocobase/utils/client';
|
||||||
import type { InputProps } from 'antd/lib/input';
|
import type { InputProps } from 'antd/lib/input';
|
||||||
import type { InputNumberProps } from 'antd/lib/input-number';
|
import type { InputNumberProps } from 'antd/lib/input-number';
|
||||||
import { toFixed } from 'rc-input-number/lib/utils/MiniDecimal';
|
|
||||||
import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil';
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
export function toFixedByStep(value: any, step: string | number) {
|
|
||||||
if (typeof value === 'undefined' || value === null || value === '') {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
const precision = getNumberPrecision(step);
|
|
||||||
// return parseFloat(String(value)).toFixed(precision);
|
|
||||||
return toFixed(String(value), '.', precision);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ReadPretty: React.FC<InputProps & InputNumberProps> = (props: any) => {
|
export const ReadPretty: React.FC<InputProps & InputNumberProps> = (props: any) => {
|
||||||
const { step, value, addonBefore, addonAfter } = props;
|
const { step, value, addonBefore, addonAfter } = props;
|
||||||
if (!isValid(props.value)) {
|
if (!isValid(props.value)) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { toFixedByStep } from '../ReadPretty';
|
import { toFixedByStep } from '@nocobase/utils/client';
|
||||||
|
|
||||||
describe('toFixedByStep', () => {
|
describe('toFixedByStep', () => {
|
||||||
it('less than precision', () => {
|
it('less than precision', () => {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export * from './merge';
|
export * from './merge';
|
||||||
|
export * from './number';
|
||||||
export * from './registry';
|
export * from './registry';
|
||||||
export * from './uid';
|
export * from './uid';
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
export * from './merge';
|
export * from './merge';
|
||||||
export * from './mixin';
|
export * from './mixin';
|
||||||
export * from './mixin/AsyncEmitter';
|
export * from './mixin/AsyncEmitter';
|
||||||
|
export * from './number';
|
||||||
export * from './registry';
|
export * from './registry';
|
||||||
export * from './requireModule';
|
export * from './requireModule';
|
||||||
export * from './uid';
|
export * from './uid';
|
||||||
|
|
||||||
|
11
packages/core/utils/src/number.ts
Normal file
11
packages/core/utils/src/number.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { toFixed } from 'rc-input-number/lib/utils/MiniDecimal';
|
||||||
|
import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil';
|
||||||
|
|
||||||
|
export function toFixedByStep(value: any, step: string | number) {
|
||||||
|
if (typeof value === 'undefined' || value === null || value === '') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
const precision = getNumberPrecision(step);
|
||||||
|
// return parseFloat(String(value)).toFixed(precision);
|
||||||
|
return toFixed(String(value), '.', precision);
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
export * from './merge';
|
export * from './merge';
|
||||||
export * from './mixin';
|
export * from './mixin';
|
||||||
export * from './mixin/AsyncEmitter';
|
export * from './mixin/AsyncEmitter';
|
||||||
|
export * from './number';
|
||||||
export * from './registry';
|
export * from './registry';
|
||||||
export * from './requireModule';
|
export * from './requireModule';
|
||||||
export * from './uid';
|
export * from './uid';
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { toFixedByStep } from '@nocobase/utils';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
export async function _(field, row, ctx, column?: any) {
|
export async function _(field, row, ctx, column?: any) {
|
||||||
@ -38,7 +39,8 @@ export async function datetime(field, row, ctx) {
|
|||||||
|
|
||||||
export async function percent(field, row, ctx) {
|
export async function percent(field, row, ctx) {
|
||||||
const value = row.get(field.name);
|
const value = row.get(field.name);
|
||||||
return value && `${value}%`;
|
const step = field.options?.uiSchema?.['x-component-props']?.['step'] ?? 0;
|
||||||
|
return value && `${toFixedByStep(value * 100, step)}%`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function boolean(field, row, ctx, column?: any) {
|
export async function boolean(field, row, ctx, column?: any) {
|
||||||
|
Loading…
Reference in New Issue
Block a user