<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mark Cohen</title>
    <description>The latest articles on DEV Community by Mark Cohen (@theshippingsavers).</description>
    <link>https://dev.to/theshippingsavers</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4187648%2Fe3a15ee5-cb1b-4d8d-8de0-2f4ca519aa60.png</url>
      <title>DEV Community: Mark Cohen</title>
      <link>https://dev.to/theshippingsavers</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/theshippingsavers"/>
    <language>en</language>
    <item>
      <title>Calculating dimensional weight for UPS and FedEx shipments in JavaScript</title>
      <dc:creator>Mark Cohen</dc:creator>
      <pubDate>Tue, 27 Oct 2026 05:54:21 +0000</pubDate>
      <link>https://dev.to/theshippingsavers/calculating-dimensional-weight-for-ups-and-fedex-shipments-in-javascript-2694</link>
      <guid>https://dev.to/theshippingsavers/calculating-dimensional-weight-for-ups-and-fedex-shipments-in-javascript-2694</guid>
      <description>&lt;p&gt;If you build checkout, fulfillment or label tooling for an e-commerce store, sooner or later someone asks why the shipping quote didn't match the carrier invoice. Nine times out of ten the answer is &lt;strong&gt;dimensional (DIM) weight&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Carriers don't just bill by what a box weighs. They bill by the greater of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;strong&gt;actual weight&lt;/strong&gt;, and&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;dimensional weight&lt;/strong&gt;, which is based on the box's size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how to compute it correctly, and the edge cases that trip up most implementations.&lt;/p&gt;

&lt;h2&gt;
  
  
  The formula
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dim_weight = (length × width × height) / divisor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All dimensions are in inches and the result is in pounds. For most domestic UPS and FedEx daily-rate shipments the divisor is &lt;strong&gt;139&lt;/strong&gt;. (USPS uses &lt;strong&gt;166&lt;/strong&gt;, and only for packages over one cubic foot.) Divisors can differ by account and contract, so keep it configurable.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small, testable implementation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Billable weight for a parcel, in whole pounds.
 * @param {object} p
 * @param {number} p.lengthIn  - inches
 * @param {number} p.widthIn   - inches
 * @param {number} p.heightIn  - inches
 * @param {number} p.weightLb  - actual weight in pounds
 * @param {number} [p.divisor=139]
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;billableWeight&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;lengthIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;widthIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;heightIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;weightLb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;divisor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;139&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Carriers measure to whole inches, so round each side first.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;lengthIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;widthIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;heightIn&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dim&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;divisor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Both values round UP to the next whole pound before comparing.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dimLb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dim&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;actualLb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;weightLb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;dimWeightLb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;dimLb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;actualWeightLb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;actualLb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;billableLb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dimLb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;actualLb&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;billedOnDim&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;dimLb&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;actualLb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;billableWeight&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;lengthIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;widthIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;heightIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;weightLb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="c1"&gt;// { dimWeightLb: 22, actualWeightLb: 6, billableLb: 22, billedOnDim: true }&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;billableWeight&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;lengthIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;widthIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;heightIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;weightLb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="c1"&gt;// { dimWeightLb: 13, actualWeightLb: 6, billableLb: 13, billedOnDim: true }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same 6 lb product, two box sizes: 22 lb billed versus 13 lb billed. That gap is real money on every order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Edge cases worth handling
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Rounding order matters.&lt;/strong&gt; Round each dimension to a whole inch first, then compute, then round the weight up. Doing it in a different order will drift from the invoice by a pound on some packages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store the sealed box size, not the product size.&lt;/strong&gt; Most "my quote was wrong" bugs come from using product dimensions from the catalog instead of the carton actually used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insulated and perishable shipments.&lt;/strong&gt; Foam coolers plus gel packs or dry ice make boxes big and light, so cold-chain shippers are billed on DIM weight almost every time. Flag &lt;code&gt;billedOnDim&lt;/code&gt; in your UI so ops teams can see it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the divisor per carrier and per account.&lt;/strong&gt; Negotiated contracts sometimes change it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Carriers re-measure.&lt;/strong&gt; If your stored dimensions are wrong, the carrier will bill the correction later as an adjustment line, often weeks after the order.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Turning it into a report
&lt;/h2&gt;

&lt;p&gt;A useful thing to give an operations team is a simple monthly report: for each SKU and box combination, how many packages were billed on DIM weight and by how much. Right-sizing the top few boxes is often the cheapest shipping saving a store can make, before even talking about rates.&lt;/p&gt;




&lt;p&gt;I work on shipping costs at &lt;a href="https://theshippingsavers.com" rel="noopener noreferrer"&gt;The Shipping Savers&lt;/a&gt;, where we review e-commerce and perishable shippers' UPS and FedEx invoices line by line. If you want the non-code version, we also wrote a guide on &lt;a href="https://theshippingsavers.com/blog/guides/how-to-read-fedex-invoice" rel="noopener noreferrer"&gt;how to read a FedEx invoice&lt;/a&gt;, including where DIM weight and adjustments show up on the bill.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ecommerce</category>
      <category>shipping</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
