Skip to content

Commit

Permalink
Merge pull request #791 from odisha-muktasoft/UCEM-1030-UI
Browse files Browse the repository at this point in the history
UCEM-1030 : updated the multiplication logic to handle decimal values…
  • Loading branch information
Tulika-eGov authored Nov 19, 2024
2 parents 04ac964 + 2da9f89 commit 3f93213
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@egovernments/digit-ui-module-dss": "1.5.52",
"@egovernments/digit-ui-module-attendencemgmt": "0.4.15",
"@egovernments/digit-ui-module-contracts": "0.4.15",
"@egovernments/digit-ui-module-measurement":"0.2.18",
"@egovernments/digit-ui-module-measurement":"0.2.19",
"@egovernments/digit-ui-module-estimate": "0.4.22",
"@egovernments/digit-ui-module-masters": "0.4.23",
"@egovernments/digit-ui-module-project": "0.4.17",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egovernments/digit-ui-module-measurement",
"version": "0.2.18",
"version": "0.2.19",
"description": "Measurement Module UI",
"main": "dist/index.js",
"module": "dist/index.modern.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,38 @@ var multiply = function(num1, num2) {
};

const multiplyWithDecimals = (v1, v2) => {
const getDecimalPlaces = num => (num.includes('.') ? num.split('.')[1].length : 0);

const getDecimalPlaces = num => {
return num.includes('.') ? num.split('.')[1].length : 0;
};

const d1 = getDecimalPlaces(v1);
const d2 = getDecimalPlaces(v2);

// Remove decimals from both numbers
const num1 = v1.replace('.', '');
const num2 = v2.replace('.', '');

// Multiply as whole numbers
let result = multiply(num1, num2);
const result = (BigInt(num1) * BigInt(num2)).toString(); // Use BigInt for accurate multiplication

// Insert decimal point at the correct place
const totalDecimals = d1 + d2;
if (totalDecimals > 0) {
const pointPos = result.length - totalDecimals;
result = result.padStart(totalDecimals + 1, '0'); // Ensure result has enough length
result = result.slice(0, pointPos) + '.' + result.slice(pointPos);
const resultLength = result.length;
const pointPos = resultLength - totalDecimals;

// Handle cases where result length is shorter than decimal places
const paddedResult = result.padStart(totalDecimals, '0'); // Ensure enough digits for decimals
const integerPart = paddedResult.slice(0, pointPos > 0 ? pointPos : 0);
const fractionalPart = paddedResult.slice(pointPos > 0 ? pointPos : 0);

// Combine integer and fractional parts
let formattedResult = (integerPart || '0') + (fractionalPart ? '.' + fractionalPart : '');

// Remove unnecessary leading and trailing zeros
formattedResult = parseFloat(formattedResult).toString();

return formattedResult;
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion frontend/micro-ui/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@egovernments/digit-ui-module-dss": "1.5.52",
"@egovernments/digit-ui-module-attendencemgmt": "0.4.15",
"@egovernments/digit-ui-module-contracts": "0.4.15",
"@egovernments/digit-ui-module-measurement":"0.2.18",
"@egovernments/digit-ui-module-measurement":"0.2.19",
"@egovernments/digit-ui-module-estimate": "0.4.22",
"@egovernments/digit-ui-module-masters": "0.4.23",
"@egovernments/digit-ui-module-project": "0.4.17",
Expand Down

0 comments on commit 3f93213

Please sign in to comment.