Using Math.trunc for integers results in -1

In the following code, when order.total_price_with_tax is 1650 and order.single_number_of_order_item is 1, I want to set the value of formattedUnitPrice and formattedTotalPrice to 1,500.

For some reason, the output is 1,499.

Math.trunc is used because the result of the UnitPrice calculation may include a decimal point, so we want to truncate the decimal point and extract an integer multiplied by the number of itemsCount.

function formatCurrency(number) {
  const formattedNumber = number.toLocaleString('ja-JP');
  return formattedNumber;
}

function generateTableRow(order) {
  const formattedOrderDate = formatDate(order.order_date);
  const nextDay = new Date(formattedOrderDate);
  nextDay.setDate(nextDay.getDate() + 1);
  const nextDayFormatted = `${nextDay.getFullYear()}/${String(nextDay.getMonth() + 1).padStart(2, '0')}/${String(nextDay.getDate()).padStart(2, '0')}`;

  const boxCount = order.dars_number_of_order_item + order.item_leaflet;
  const unitPriceWithoutTax = order.single_number_of_order_item > 0 ?
    Math.trunc(order.total_price_with_tax / order.single_number_of_order_item / 1.1) :
    (order.dars_number_of_order_item > 0 ?
    Math.trunc(order.total_price_with_tax / order.dars_number_of_order_item / 1.1) :
    Math.trunc(order.total_price_with_tax / order.item_leaflet / 1.1));
  const itemCount = order.single_number_of_order_item > 0 ? 
    order.single_number_of_order_item  :
      (order.dars_number_of_order_item > 0 ?
      order.dars_number_of_order_item :
      order.item_leaflet);

  const formattedUnitPrice = formatCurrency(unitPriceWithoutTax);
  const formattedTotalPrice = formatCurrency(unitPriceWithoutTax * itemCount);

  return `
    <tr>
      <td style="text-align:center;">${nextDayFormatted}</td>
      <td style="text-align:left;">${order.item_name}</td>
      <td>${boxCount}</td>
      <td>${order.total_item_num}</td>
      <td>${order.unit}</td>
      <td>${formattedUnitPrice}</td>
      <td>${formattedTotalPrice}</td>
    </tr>
  `;
}

Hi @nocode_papa, I am sorry you’re having trouble.

Is there a chance you have posted this on the wrong forum by any chance? I can’t seem to find any n8n specific problem in your post.

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