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. 60
      packages/nocodb-sdk/src/lib/helperFunctions.ts

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

@ -1,5 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { VNodeRef } from '@vue/runtime-core' import type { VNodeRef } from '@vue/runtime-core'
import { roundUpToPrecision } from 'nocodb-sdk'
interface Props { interface Props {
// when we set a number, then it is number type // when we set a number, then it is number type
@ -35,13 +36,6 @@ const meta = computed(() => {
const _vModel = useVModel(props, 'modelValue', emits) 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(() => { const displayValue = computed(() => {
if (_vModel.value === null) return null if (_vModel.value === null) return null

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

@ -108,31 +108,46 @@ const getAvailableRollupForUiType = (type: string) => {
} }
}; };
const getFileName = ({ const getFileName = ({ name, count, ext }) =>
name, `${name}${count ? `(${count})` : ''}${ext ? `${ext}` : ''}`;
count,
ext
}) => `${name}${count ? `(${count})` : ''}${ext? `${ext}` : ''}`
// add count before extension if duplicate name found // add count before extension if duplicate name found
function populateUniqueFileName( function populateUniqueFileName(fileName: string, attachments: string[]) {
fileName: string, return fileName.replace(
attachments: string[] /^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/,
) { (fileName, name, count, ext) => {
return fileName.replace(/^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/, (fileName,name, count, ext) =>{ let genFileName = fileName;
let genFileName = fileName; let c = count || 1;
let c = count || 1;
// iterate until a unique name
// iterate until a unique name while (attachments.some((fn) => fn === genFileName)) {
while (attachments.some((fn) => fn === genFileName)) { genFileName = getFileName({
genFileName = getFileName({ name,
name, ext,
ext, count: c++,
count: c++ });
}); }
return genFileName;
} }
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 { export {
@ -145,4 +160,5 @@ export {
stringifyRolesObj, stringifyRolesObj,
getAvailableRollupForUiType, getAvailableRollupForUiType,
populateUniqueFileName, populateUniqueFileName,
roundUpToPrecision,
}; };

Loading…
Cancel
Save