Browse Source

Merge pull request #8874 from nocodb/nc-fix/decimal-roundup-to-precision-issue

fix: update roundUpToPrecision function
pull/8877/head
Ramesh Mane 5 months ago committed by GitHub
parent
commit
843f2d73a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      packages/nc-gui/components/cell/Decimal.vue
  2. 40
      packages/nocodb-sdk/src/lib/helperFunctions.ts

8
packages/nc-gui/components/cell/Decimal.vue

@ -1,5 +1,6 @@
<script lang="ts" setup>
import type { VNodeRef } from '@vue/runtime-core'
import { roundUpToPrecision } from 'nocodb-sdk'
interface Props {
// when we set a number, then it is number type
@ -35,13 +36,6 @@ const meta = computed(() => {
const _vModel = useVModel(props, 'modelValue', emits)
function roundUpToPrecision(value: number, precision = 1) {
const factor = Math.pow(10, precision)
const roundedValue = Math.round(value * factor) / factor
return roundedValue.toFixed(precision)
}
const displayValue = computed(() => {
if (_vModel.value === null) return null

40
packages/nocodb-sdk/src/lib/helperFunctions.ts

@ -108,18 +108,14 @@ const getAvailableRollupForUiType = (type: string) => {
}
};
const getFileName = ({
name,
count,
ext
}) => `${name}${count ? `(${count})` : ''}${ext? `${ext}` : ''}`
const getFileName = ({ name, count, ext }) =>
`${name}${count ? `(${count})` : ''}${ext ? `${ext}` : ''}`;
// add count before extension if duplicate name found
function populateUniqueFileName(
fileName: string,
attachments: string[]
) {
return fileName.replace(/^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/, (fileName,name, count, ext) =>{
function populateUniqueFileName(fileName: string, attachments: string[]) {
return fileName.replace(
/^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/,
(fileName, name, count, ext) => {
let genFileName = fileName;
let c = count || 1;
@ -128,11 +124,30 @@ function populateUniqueFileName(
genFileName = getFileName({
name,
ext,
count: c++
count: c++,
});
}
return genFileName;
});
}
);
}
function roundUpToPrecision(number: number, precision: number = 0) {
precision =
precision == null
? 0
: precision >= 0
? Math.min(precision, 292)
: Math.max(precision, -292);
if (precision) {
// Shift with exponential notation to avoid floating-point issues.
// See [MDN](https://mdn.io/round#Examples) for more details.
let pair = `${number}e`.split('e');
const value = Math.round(Number(`${pair[0]}e${+pair[1] + precision}`));
pair = `${value}e`.split('e');
return +`${pair[0]}e${+pair[1] - precision}`;
}
return Math.round(number);
}
export {
@ -145,4 +160,5 @@ export {
stringifyRolesObj,
getAvailableRollupForUiType,
populateUniqueFileName,
roundUpToPrecision,
};

Loading…
Cancel
Save