diff --git a/src/components/desktop/PieChart.vue b/src/components/desktop/PieChart.vue index 00b4a411..3a046810 100644 --- a/src/components/desktop/PieChart.vue +++ b/src/components/desktop/PieChart.vue @@ -10,7 +10,7 @@ import { mapStores } from 'pinia'; import { useSettingsStore } from '@/stores/setting.js'; import colorConstants from '@/consts/color.js'; -import { formatPercent } from '@/lib/common.js'; +import { formatPercent } from '@/lib/numeral.js'; export default { props: [ diff --git a/src/components/mobile/NumberPadSheet.vue b/src/components/mobile/NumberPadSheet.vue index ea99fbba..0f750db7 100644 --- a/src/components/mobile/NumberPadSheet.vue +++ b/src/components/mobile/NumberPadSheet.vue @@ -66,7 +66,8 @@ import { mapStores } from 'pinia'; import { useSettingsStore } from '@/stores/setting.js'; -import { isString, appendThousandsSeparator } from '@/lib/common.js'; +import { isString } from '@/lib/common.js'; +import { appendThousandsSeparator } from '@/lib/numeral.js'; import { numericCurrencyToString, stringCurrencyToNumeric } from '@/lib/currency.js'; export default { diff --git a/src/components/mobile/PieChart.vue b/src/components/mobile/PieChart.vue index 25c308de..be0daa1b 100644 --- a/src/components/mobile/PieChart.vue +++ b/src/components/mobile/PieChart.vue @@ -81,7 +81,7 @@ import { mapStores } from 'pinia'; import { useSettingsStore } from '@/stores/setting.js'; import colorConstants from '@/consts/color.js'; -import { formatPercent } from '@/lib/common.js'; +import { formatPercent } from '@/lib/numeral.js'; export default { props: [ diff --git a/src/lib/common.js b/src/lib/common.js index d2457673..bb089a3f 100644 --- a/src/lib/common.js +++ b/src/lib/common.js @@ -109,58 +109,6 @@ export function getObjectOwnFieldCount(object) { return count; } -export function appendThousandsSeparator(value, enable) { - if (!enable || value.length <= 3) { - return value; - } - - const negative = value.charAt(0) === '-'; - - if (negative) { - value = value.substring(1); - } - - const dotPos = value.indexOf('.'); - const integer = dotPos < 0 ? value : value.substring(0, dotPos); - const decimals = dotPos < 0 ? '' : value.substring(dotPos + 1, value.length); - - const finalChars = []; - - for (let i = 0; i < integer.length; i++) { - if (i % 3 === 0 && i > 0) { - finalChars.push(','); - } - - finalChars.push(integer.charAt(integer.length - 1 - i)); - } - - finalChars.reverse(); - - let newInteger = finalChars.join(''); - - if (negative) { - newInteger = `-${newInteger}`; - } - - if (dotPos < 0) { - return newInteger; - } else { - return `${newInteger}.${decimals}`; - } -} - -export function formatPercent(value, precision, lowPrecisionValue) { - const ratio = Math.pow(10, precision); - const normalizedValue = Math.floor(value * ratio); - - if (value > 0 && normalizedValue < 1 && lowPrecisionValue) { - return lowPrecisionValue + '%'; - } - - const result = normalizedValue / ratio; - return result + '%'; -} - export function limitText(value, maxLength) { let length = 0; diff --git a/src/lib/currency.js b/src/lib/currency.js index 317de559..647a7a53 100644 --- a/src/lib/currency.js +++ b/src/lib/currency.js @@ -1,4 +1,5 @@ -import { isNumber, appendThousandsSeparator } from './common.js'; +import { isNumber } from './common.js'; +import { appendThousandsSeparator } from './numeral.js'; export function numericCurrencyToString(num, enableThousandsSeparator, trimTailZero) { let str = num.toString(); diff --git a/src/lib/numeral.js b/src/lib/numeral.js new file mode 100644 index 00000000..a62c26dc --- /dev/null +++ b/src/lib/numeral.js @@ -0,0 +1,51 @@ +export function appendThousandsSeparator(value, enable) { + if (!enable || value.length <= 3) { + return value; + } + + const negative = value.charAt(0) === '-'; + + if (negative) { + value = value.substring(1); + } + + const dotPos = value.indexOf('.'); + const integer = dotPos < 0 ? value : value.substring(0, dotPos); + const decimals = dotPos < 0 ? '' : value.substring(dotPos + 1, value.length); + + const finalChars = []; + + for (let i = 0; i < integer.length; i++) { + if (i % 3 === 0 && i > 0) { + finalChars.push(','); + } + + finalChars.push(integer.charAt(integer.length - 1 - i)); + } + + finalChars.reverse(); + + let newInteger = finalChars.join(''); + + if (negative) { + newInteger = `-${newInteger}`; + } + + if (dotPos < 0) { + return newInteger; + } else { + return `${newInteger}.${decimals}`; + } +} + +export function formatPercent(value, precision, lowPrecisionValue) { + const ratio = Math.pow(10, precision); + const normalizedValue = Math.floor(value * ratio); + + if (value > 0 && normalizedValue < 1 && lowPrecisionValue) { + return lowPrecisionValue + '%'; + } + + const result = normalizedValue / ratio; + return result + '%'; +} diff --git a/src/views/desktop/statistics/TransactionPage.vue b/src/views/desktop/statistics/TransactionPage.vue index 9dacea26..f1320dc8 100644 --- a/src/views/desktop/statistics/TransactionPage.vue +++ b/src/views/desktop/statistics/TransactionPage.vue @@ -302,7 +302,8 @@ import { useStatisticsStore } from '@/stores/statistics.js'; import datetimeConstants from '@/consts/datetime.js'; import statisticsConstants from '@/consts/statistics.js'; -import { limitText, formatPercent } from '@/lib/common.js' +import { limitText } from '@/lib/common.js' +import { formatPercent } from '@/lib/numeral.js'; import { getYearAndMonthFromUnixTime, getYearMonthFirstUnixTime, diff --git a/src/views/desktop/user/settings/tabs/UserDataManagementSettingTab.vue b/src/views/desktop/user/settings/tabs/UserDataManagementSettingTab.vue index d4aebc49..4f5fff0b 100644 --- a/src/views/desktop/user/settings/tabs/UserDataManagementSettingTab.vue +++ b/src/views/desktop/user/settings/tabs/UserDataManagementSettingTab.vue @@ -166,7 +166,8 @@ import { useRootStore } from '@/stores/index.js'; import { useSettingsStore } from '@/stores/setting.js'; import { useUserStore } from '@/stores/user.js'; -import {appendThousandsSeparator, isEquals} from '@/lib/common.js'; +import { isEquals } from '@/lib/common.js'; +import { appendThousandsSeparator } from '@/lib/numeral.js'; import { isDataExportingEnabled } from '@/lib/server_settings.js'; import { startDownloadFile } from '@/lib/ui.js'; diff --git a/src/views/mobile/statistics/TransactionPage.vue b/src/views/mobile/statistics/TransactionPage.vue index bb6dd142..8bca772c 100644 --- a/src/views/mobile/statistics/TransactionPage.vue +++ b/src/views/mobile/statistics/TransactionPage.vue @@ -258,7 +258,8 @@ import { useStatisticsStore } from '@/stores/statistics.js'; import datetimeConstants from '@/consts/datetime.js'; import statisticsConstants from '@/consts/statistics.js'; -import { getNameByKeyValue, limitText, formatPercent } from '@/lib/common.js' +import { getNameByKeyValue, limitText } from '@/lib/common.js' +import { formatPercent } from '@/lib/numeral.js' import { getShiftedDateRangeAndDateType, getDateTypeByDateRange, diff --git a/src/views/mobile/users/DataManagementPage.vue b/src/views/mobile/users/DataManagementPage.vue index cd5ca274..658e142a 100644 --- a/src/views/mobile/users/DataManagementPage.vue +++ b/src/views/mobile/users/DataManagementPage.vue @@ -73,7 +73,7 @@ import { useRootStore } from '@/stores/index.js'; import { useSettingsStore } from '@/stores/setting.js'; import { useUserStore } from '@/stores/user.js'; -import { appendThousandsSeparator } from '@/lib/common.js'; +import { appendThousandsSeparator } from '@/lib/numeral.js'; import { isDataExportingEnabled } from '@/lib/server_settings.js'; export default {