From 8b2d5872657b4dc6bc8d6a2f2d64ae1fe3ee721a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez=20Mu=C3=B1oz?= Date: Tue, 10 Sep 2024 16:34:47 +0200 Subject: [PATCH] avoids floating point --- .../scenario/grid-setup/features/target-spf/index.tsx | 7 ++++--- app/utils/numbers.ts | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 app/utils/numbers.ts diff --git a/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/index.tsx b/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/index.tsx index e51764cd5a..e35c3d46b7 100644 --- a/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/index.tsx +++ b/app/layout/project/sidebar/scenario/grid-setup/features/target-spf/index.tsx @@ -24,6 +24,7 @@ import TargetsSPFTable from 'layout/project/sidebar/scenario/grid-setup/features import ActionsMenu from 'layout/project/sidebar/scenario/grid-setup/features/target-spf/targets-spf-table/actions-menu'; import Section from 'layout/section'; import { Feature } from 'types/api/feature'; +import { toFixedWithoutZeros } from 'utils/numbers'; import CLOSE_SVG from 'svgs/ui/close.svg?sprite'; @@ -103,7 +104,7 @@ const TargetAndSPFFeatures = (): JSX.Element => { splitted: true, marxanSettings: { ...splitFeature.marxanSettings, - prop: splitFeature.marxanSettings?.prop * 100, + prop: toFixedWithoutZeros(splitFeature.marxanSettings?.prop * 100), ...(featureValues[`${feature.id}-${splitFeature.name}`]?.target && { prop: featureValues[`${feature.id}-${splitFeature.name}`].target, }), @@ -127,7 +128,7 @@ const TargetAndSPFFeatures = (): JSX.Element => { type: featureMetadata?.tag, marxanSettings: { ...feature.marxanSettings, - prop: feature.marxanSettings?.prop * 100, + prop: toFixedWithoutZeros(feature.marxanSettings?.prop * 100), ...(featureValues[feature.id]?.target && { prop: featureValues[feature.id].target, }), @@ -424,7 +425,7 @@ const TargetAndSPFFeatures = (): JSX.Element => { return { ...acc, [featureId]: { - target: marxanSettings?.prop * 100, + target: toFixedWithoutZeros(marxanSettings?.prop * 100), spf: marxanSettings?.fpf, }, }; diff --git a/app/utils/numbers.ts b/app/utils/numbers.ts new file mode 100644 index 0000000000..8c24eb8cc7 --- /dev/null +++ b/app/utils/numbers.ts @@ -0,0 +1,3 @@ +export function toFixedWithoutZeros(num: number, decimalPlaces = 2): number { + return Number(num.toFixed(decimalPlaces).replace(/\.?0+$/, '')); +}