Browse Source

feat(gui-v2): add percent utils

pull/2910/head
Wing-Kam Wong 2 years ago
parent
commit
23287e5547
  1. 30
      packages/nc-gui-v2/utils/percentUtils.ts

30
packages/nc-gui-v2/utils/percentUtils.ts

@ -0,0 +1,30 @@
export const precisions = [
{ id: 0, title: '1' },
{ id: 1, title: '1.0' },
{ id: 2, title: '1.00' },
{ id: 3, title: '1.000' },
{ id: 4, title: '1.0000' },
{ id: 5, title: '1.00000' },
{ id: 6, title: '1.000000' },
{ id: 7, title: '1.0000000' },
{ id: 8, title: '1.00000000' },
]
export function renderPercent(value: any, precision: number, withPercentSymbol = true) {
if (!value) return value
value = (Number(value) * 100).toFixed(precision)
if (withPercentSymbol) return padPercentSymbol(value)
return value
}
export function isValidPercent(value: any, negative: boolean): boolean {
return negative ? /^-?\d{1,20}(\.\d+)?$/.test(value) : /^\d{1,20}(\.\d+)?$/.test(value)
}
export function getPercentStep(precision: number): string {
return (1 / 10 ** precision).toString()
}
function padPercentSymbol(value: any) {
return value ? `${value}%` : value
}
Loading…
Cancel
Save