Skip to content

Commit

Permalink
avoids floating point
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgnlez committed Sep 10, 2024
1 parent bead9ce commit 8b2d587
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
}),
Expand All @@ -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,
}),
Expand Down Expand Up @@ -424,7 +425,7 @@ const TargetAndSPFFeatures = (): JSX.Element => {
return {
...acc,
[featureId]: {
target: marxanSettings?.prop * 100,
target: toFixedWithoutZeros(marxanSettings?.prop * 100),
spf: marxanSettings?.fpf,
},
};
Expand Down
3 changes: 3 additions & 0 deletions app/utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function toFixedWithoutZeros(num: number, decimalPlaces = 2): number {
return Number(num.toFixed(decimalPlaces).replace(/\.?0+$/, ''));
}

0 comments on commit 8b2d587

Please sign in to comment.