Convert a string to number

Hi.
I have a function node with code which works as it should. But our team member wants an calculation based on what it seems to be 2 string. Is this possible to do and at the same time output it again as the string (since the api request need this when using the output as invoice rows?)

you can see in the line . This outputs f.eks. 10 / 2 , instead of the answer 5 which we wants.

// hjelpefunksjon for xml-entiteter
const escapeXml = (unsafe) =>
unsafe.replace(/[<>&'"]/g, (c) => &${({ '<': 'lt', '>': 'gt', '&': 'amp', '\'': 'apos', '"': 'quot' })[c]};);

// sorter input i kunder og id-er for produkter
const lookupId = {};
const customers = [];

for (const item of $input.all()) {
if (item.json.Charges) {
customers.push(item);
} else if (item.json.Id && item.json.No) {
lookupId[item.json.No] = item.json.Id;
}
}

// Lag XML for ordrelinjer
for (const customer of customers) {
customer.json.chargeXml = ‘’;
customer.json.Charges.forEach((charge) => {
customer.json.chargeXml +=
<InvoiceRow> <ProductId>${lookupId[charge.MaterialNumber] ? lookupId[charge.MaterialNumber] : charge.MaterialNumber}</ProductId> <Price>${charge.CorrectSales} / ${charge.UDRCValue}</Price> <InPrice>${charge.CorrectCosts}</InPrice> <Quantity>${charge.UDRCValue}</Quantity> <Name>${escapeXml(charge.PriceableItemDescription)}</Name> </InvoiceRow>;
});
}

return customers;

Hi @frankemann, looks like your <Price> portion reads <Price>${charge.CorrectSales} / ${charge.UDRCValue}</Price>.

In JavaScript, you can convert string such as "10" or "2" proper into integers using the parseInt() function.

So as a first step you might want to try replacing the above with something like this:

<Price>${parseInt(charge.CorrectSales) / parseInt(charge.UDRCValue)}</Price>

On a separate note, when posting code on the forum you want to use the functionality for preformatted text. This will preserve the formatting and make sure your question is fully readable.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.