<?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: lei zhang</title>
    <description>The latest articles on DEV Community by lei zhang (@lei_zhang_b6e446ccf73eac3).</description>
    <link>https://dev.to/lei_zhang_b6e446ccf73eac3</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%2F4203026%2Fbdbbfc5f-429b-477c-879a-e3d767bd4b7a.png</url>
      <title>DEV Community: lei zhang</title>
      <link>https://dev.to/lei_zhang_b6e446ccf73eac3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lei_zhang_b6e446ccf73eac3"/>
    <language>en</language>
    <item>
      <title>CSV vs CSV UTF-8: the same rows in four encodings, byte by byte</title>
      <dc:creator>lei zhang</dc:creator>
      <pubDate>Thu, 12 Nov 2026 00:15:14 +0000</pubDate>
      <link>https://dev.to/lei_zhang_b6e446ccf73eac3/csv-vs-csv-utf-8-the-same-rows-in-four-encodings-byte-by-byte-1ik7</link>
      <guid>https://dev.to/lei_zhang_b6e446ccf73eac3/csv-vs-csv-utf-8-the-same-rows-in-four-encodings-byte-by-byte-1ik7</guid>
      <description>&lt;p&gt;"CSV" and "CSV UTF-8" sound like two formats. They are the same format: CSV describes the rows and columns, UTF-8 describes how the characters are stored as bytes. The only other thing that differs is an optional three-byte mark at the start of the file.&lt;/p&gt;

&lt;p&gt;To see exactly what changes, I saved the same three rows (&lt;code&gt;Zoë Müller&lt;/code&gt;, &lt;code&gt;José Peña&lt;/code&gt;, &lt;code&gt;Chloé Dubois&lt;/code&gt;) in four encodings and read the bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same rows, four encodings
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Encoding&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;First bytes&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;ë&lt;/code&gt; is stored as&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8 with BOM&lt;/td&gt;
&lt;td&gt;88 bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;EF BB BF 69 64 2C&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;C3 AB&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8, no BOM&lt;/td&gt;
&lt;td&gt;85 bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;69 64 2C 6E 61 6D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;C3 AB&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows-1252&lt;/td&gt;
&lt;td&gt;77 bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;69 64 2C 6E 61 6D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;EB&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-16 LE with BOM&lt;/td&gt;
&lt;td&gt;156 bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;FF FE 69 00 64 00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;EB 00&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three things stand out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For plain English text, UTF-8 and Windows-1252 store identical bytes. Both files start &lt;code&gt;69 64 2C&lt;/code&gt; ("id,"). The difference only shows up with accented or non-Latin letters.&lt;/li&gt;
&lt;li&gt;The first two rows differ by exactly three bytes: &lt;code&gt;EF BB BF&lt;/code&gt;, the byte order mark (BOM). The &lt;a href="https://www.unicode.org/faq/utf_bom.html" rel="noopener noreferrer"&gt;Unicode FAQ&lt;/a&gt; describes it, for UTF-8, as a signature that identifies the encoding.&lt;/li&gt;
&lt;li&gt;Windows-1252 is smaller, but it only covers Western European letters. Asking Python to save &lt;code&gt;東京&lt;/code&gt; as Windows-1252 raises an error; with &lt;code&gt;errors="replace"&lt;/code&gt; the two characters become &lt;code&gt;??&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What a wrong guess looks like
&lt;/h2&gt;

&lt;p&gt;A CSV file on disk does not say which encoding it uses (&lt;a href="https://www.rfc-editor.org/rfc/rfc4180" rel="noopener noreferrer"&gt;RFC 4180&lt;/a&gt; leaves the character set to a MIME parameter), so software guesses. A wrong guess gives one of two recognisable patterns:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You see&lt;/th&gt;
&lt;th&gt;What happened&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ZoÃ« MÃ¼ller&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;UTF-8 bytes read as Windows-1252&lt;/td&gt;
&lt;td&gt;Re-open as UTF-8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Zo� M�ller&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Windows-1252 bytes read as UTF-8&lt;/td&gt;
&lt;td&gt;Re-open as Windows-1252&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first can be undone by re-reading the original. If a program has already saved the file with replacement characters, those letters are gone from that copy.&lt;/p&gt;

&lt;h2&gt;
  
  
  The BOM cuts both ways
&lt;/h2&gt;

&lt;p&gt;Microsoft says a UTF-8 CSV opens normally in Excel &lt;a href="https://support.microsoft.com/en-gb/excel/opening-csv-utf-8-files-correctly-in-excel" rel="noopener noreferrer"&gt;if it was saved with the mark&lt;/a&gt;. Scripts are a different story. Python 3.9.6 with &lt;code&gt;csv.DictReader&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File&lt;/th&gt;
&lt;th&gt;Opened as&lt;/th&gt;
&lt;th&gt;First column name&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8 with BOM&lt;/td&gt;
&lt;td&gt;&lt;code&gt;utf-8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;\ufeffid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;row["id"]&lt;/code&gt; fails: the name has an invisible character&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8 with BOM&lt;/td&gt;
&lt;td&gt;&lt;code&gt;utf-8-sig&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Correct&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UTF-8, no BOM&lt;/td&gt;
&lt;td&gt;&lt;code&gt;utf-8-sig&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Correct, so this setting is safe for both&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows-1252&lt;/td&gt;
&lt;td&gt;&lt;code&gt;utf-8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;UnicodeDecodeError&lt;/code&gt; at the first &lt;code&gt;ë&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Which one to choose
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Opened by people in Excel:&lt;/strong&gt; UTF-8 with BOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code, a database or an API:&lt;/strong&gt; UTF-8 without BOM, unless its documentation says otherwise. When reading in Python, &lt;code&gt;encoding="utf-8-sig"&lt;/code&gt; handles both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A legacy system that asks for "ANSI":&lt;/strong&gt; Windows-1252, only if all your characters fit, and check for lost characters afterwards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Excel's own "CSV (Comma delimited)" and "CSV UTF-8 (Comma delimited)" both use commas; the name before the brackets is about the encoding. Microsoft's &lt;a href="https://support.microsoft.com/en-us/office/file-formats-that-are-supported-in-excel-0943ff2c-6014-4e8d-aaea-b83d51d46247" rel="noopener noreferrer"&gt;format table&lt;/a&gt; does not name an encoding for the plain one, so check a saved file rather than trusting the name.&lt;/p&gt;

&lt;p&gt;The four test files, the full write-up and a browser tool that shows a file's encoding and saves UTF-8 with or without a BOM are on &lt;a href="https://csvlane.com/guides/csv-vs-csv-utf-8" rel="noopener noreferrer"&gt;CSVLane&lt;/a&gt;. It runs in the browser tab, so the file is not uploaded.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was prepared with AI assistance and adapted from the original article on csvlane.com; the byte counts and Python results come from the test files linked there.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>csv</category>
      <category>python</category>
    </item>
    <item>
      <title>Why your freight bill doesn't match your CBM: how chargeable weight is calculated</title>
      <dc:creator>lei zhang</dc:creator>
      <pubDate>Fri, 30 Oct 2026 13:22:51 +0000</pubDate>
      <link>https://dev.to/lei_zhang_b6e446ccf73eac3/why-your-freight-bill-doesnt-match-your-cbm-how-chargeable-weight-is-calculated-128d</link>
      <guid>https://dev.to/lei_zhang_b6e446ccf73eac3/why-your-freight-bill-doesnt-match-your-cbm-how-chargeable-weight-is-calculated-128d</guid>
      <description>&lt;p&gt;Volume is a measurement. Chargeable weight is a rule. Most of the confusion about a freight quote comes from mixing them up, so here is the arithmetic, with numbers you can check yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: the volume
&lt;/h2&gt;

&lt;p&gt;For one carton size:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CBM per carton = length x width x height (in metres)
Line CBM = CBM per carton x cartons
Shipment CBM = sum of the lines
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In centimetres, divide the product by 1,000,000. Keep one line per carton size. Averaging the sizes of different cartons gives a wrong total, because volume does not average linearly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: the billing rule
&lt;/h2&gt;

&lt;p&gt;The same volume becomes different numbers depending on the service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Air and express&lt;/strong&gt; compare gross weight with volumetric weight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Volumetric kg = total cm3 / divisor
Planning chargeable kg = max(gross kg, volumetric kg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A divisor of 5,000 corresponds to 200 kg per cubic metre. A divisor of 6,000 corresponds to about 166.67 kg per cubic metre. Neither applies to every service or route, so use the one on your quotation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sea LCL&lt;/strong&gt; usually bills on W/M: cubic metres compared with gross tonnes, whichever is greater.&lt;/p&gt;

&lt;h2&gt;
  
  
  A worked example
&lt;/h2&gt;

&lt;p&gt;Take a shipment of 0.72 m3 (720,000 cm3) weighing 120 kg gross.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Basis&lt;/th&gt;
&lt;th&gt;Calculation&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Express, divisor 5,000&lt;/td&gt;
&lt;td&gt;720,000 / 5,000 = 144 kg, against 120 kg gross&lt;/td&gt;
&lt;td&gt;144 kg&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Air, divisor 6,000&lt;/td&gt;
&lt;td&gt;720,000 / 6,000 = 120 kg, against 120 kg gross&lt;/td&gt;
&lt;td&gt;120 kg&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sea LCL, W/M&lt;/td&gt;
&lt;td&gt;0.72 m3 against 0.12 tonnes&lt;/td&gt;
&lt;td&gt;0.72 W/M&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same cartons, three different billing numbers. The goods did not change, only the rule did.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part that surprises people: rounding
&lt;/h2&gt;

&lt;p&gt;Two cartons, each 51 x 40 x 30 cm and 10 kg. At a divisor of 5,000, each one has 12.24 kg of volumetric weight, so the shipment compares 24.48 kg volumetric with 20 kg gross.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Round the whole shipment up to the next 0.5 kg: &lt;strong&gt;24.5 kg&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Round each carton up to the next 0.5 kg, then add: &lt;strong&gt;25 kg&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither is wrong. Which one applies depends on your tariff, so it is worth asking. With mixed carton sizes, adding each carton's higher weight can also differ from taking the higher of the shipment totals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check before you book
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Measure the packed outer carton, not the product.&lt;/li&gt;
&lt;li&gt;Ask for the divisor, the rounding rule and the minimum charge.&lt;/li&gt;
&lt;li&gt;Check whether the service bills per piece.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These figures are for planning, not a carrier tariff.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclosure
&lt;/h2&gt;

&lt;p&gt;I built &lt;a href="https://livnaro.cc/" rel="noopener noreferrer"&gt;Livnaro&lt;/a&gt;, a free browser CBM worksheet that does this arithmetic for mixed carton sizes and shows the formula with each result. The two-carton example is also on its &lt;a href="https://livnaro.cc/chargeable-weight-calculator" rel="noopener noreferrer"&gt;chargeable weight calculator&lt;/a&gt;, and the measuring details are in &lt;a href="https://livnaro.cc/guides/how-to-calculate-cbm" rel="noopener noreferrer"&gt;how to calculate CBM&lt;/a&gt;. This post was drafted by an AI assistant from those pages.&lt;/p&gt;

</description>
      <category>logistics</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>How a browser turns a photo into a coloring page, with no server</title>
      <dc:creator>lei zhang</dc:creator>
      <pubDate>Fri, 30 Oct 2026 11:52:38 +0000</pubDate>
      <link>https://dev.to/lei_zhang_b6e446ccf73eac3/how-a-browser-turns-a-photo-into-a-coloring-page-with-no-server-4cae</link>
      <guid>https://dev.to/lei_zhang_b6e446ccf73eac3/how-a-browser-turns-a-photo-into-a-coloring-page-with-no-server-4cae</guid>
      <description>&lt;p&gt;I built Huepad, a free tool that turns photos into printable coloring pages, and I wanted the default path to work without uploading anyone's family photos.&lt;/p&gt;

&lt;p&gt;There are two local pipelines. Lines smooths the photo, finds where brightness changes sharply, removes small specks and thickens what is left. It suits drawings, buildings and toys.&lt;/p&gt;

&lt;p&gt;Shapes suits pets and people. It flattens texture with an edge-preserving Kuwahara filter, reduces the photo to 4–16 colors with k-means, merges tiny areas into their neighbours, joins areas separated only by a soft gradient, then outlines every area left. Each space on the page ends up closed, which is what you want for coloring.&lt;/p&gt;

&lt;p&gt;Both run in a background thread of the page, so a loaded page keeps working offline. The same photo and settings always give the same page.&lt;/p&gt;

&lt;p&gt;AI lines is the one exception. It sends the photo once to a server running FLUX.2 [klein], and only after the visitor agrees. I wrote up when each mode works best here: &lt;a href="https://huepad.cc/guides/lines-shapes-or-ai-coloring-page" rel="noopener noreferrer"&gt;https://huepad.cc/guides/lines-shapes-or-ai-coloring-page&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>showdev</category>
    </item>
    <item>
      <title>A small JavaScript model for yield-adjusted recipe costs</title>
      <dc:creator>lei zhang</dc:creator>
      <pubDate>Sun, 25 Oct 2026 09:17:39 +0000</pubDate>
      <link>https://dev.to/lei_zhang_b6e446ccf73eac3/a-small-javascript-model-for-yield-adjusted-recipe-costs-1cd0</link>
      <guid>https://dev.to/lei_zhang_b6e446ccf73eac3/a-small-javascript-model-for-yield-adjusted-recipe-costs-1cd0</guid>
      <description>&lt;p&gt;A recipe-cost calculator is a useful small example of a broader programming problem: the unit you buy is not always the unit you can use.&lt;/p&gt;

&lt;p&gt;Suppose a hypothetical ingredient costs &lt;strong&gt;$10 for 1,000 g&lt;/strong&gt;, but trimming leaves &lt;strong&gt;80% usable weight&lt;/strong&gt;. If a recipe needs &lt;strong&gt;100 g of the usable ingredient&lt;/strong&gt;, its cost is $1.25, rather than $1.00.&lt;/p&gt;

&lt;p&gt;The interesting engineering work is keeping those meanings clear at the input boundary, then preserving them through validation, calculation and display.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write down the quantity basis first
&lt;/h2&gt;

&lt;p&gt;For this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;packGrams&lt;/code&gt; is the purchased weight, before the loss represented by yield.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;yieldPercent&lt;/code&gt; converts that purchased weight into usable weight.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;usedGrams&lt;/code&gt; is the usable quantity needed by the recipe.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;packPrice&lt;/code&gt; is the price for the entire purchased pack, in one chosen currency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The formula follows directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;usable pack weight = purchased pack weight × yield / 100
ingredient cost = pack price × usable recipe quantity / usable pack weight
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The denominator matters. Dividing by &lt;code&gt;0.8&lt;/code&gt; increases the cost per usable gram; multiplying the final cost by &lt;code&gt;0.8&lt;/code&gt; would make the loss look like a discount.&lt;/p&gt;

&lt;p&gt;This model is only correct when the yield and the recipe quantity describe the same preparation stage. A raw trimmed weight and a cooked weight are different bases. Do not apply a cooking yield again to an ingredient that was already entered on a cooked basis.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep a small, pure calculation function
&lt;/h2&gt;

&lt;p&gt;This standalone teaching example accepts numeric values in grams. It is not a copy of a complete application's calculation engine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ingredientCost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;packPrice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;packGrams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;yieldPercent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;usedGrams&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;packPrice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;packGrams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;yieldPercent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;usedGrams&lt;/span&gt;
  &lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; must be a finite number`&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packPrice&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;packGrams&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;usedGrams&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;yieldPercent&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;yieldPercent&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RangeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid price, mass, or yield&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&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;edibleGrams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;packGrams&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;yieldPercent&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;packPrice&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;usedGrams&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;edibleGrams&lt;/span&gt;&lt;span class="p"&gt;;&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;cost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ingredientCost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;packPrice&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;packGrams&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;yieldPercent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;usedGrams&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&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="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1.25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero usage and a zero price are meaningful inputs, so they remain valid. A zero pack weight or zero yield would make the denominator invalid. Negative quantities, non-finite values and numeric-looking strings are rejected by the calculation function.&lt;/p&gt;

&lt;p&gt;Keep DOM parsing outside this function. An empty input should stay a validation error rather than silently becoming a zero through &lt;code&gt;Number('')&lt;/code&gt;. Parse a non-empty field, check its range, and display a field-specific message before calling the calculation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normalize units before calculating
&lt;/h2&gt;

&lt;p&gt;The example deliberately does not accept arbitrary unit strings. Convert compatible mass units into grams at a separate boundary. Keep the original unit and value for display and export.&lt;/p&gt;

&lt;p&gt;Volume is another dimension. Converting a cup of an ingredient into grams requires a relevant density or measured mass. A generic conversion table cannot safely treat every cup as the same number of grams. Reject an unsupported mass/volume combination rather than returning a plausible-looking answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check behavior, not only one output
&lt;/h2&gt;

&lt;p&gt;These checks capture the meaning of the inputs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Change to the example&lt;/th&gt;
&lt;th&gt;Expected result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Yield becomes 100%&lt;/td&gt;
&lt;td&gt;$1.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usable recipe quantity doubles&lt;/td&gt;
&lt;td&gt;$2.50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usable recipe quantity becomes zero&lt;/td&gt;
&lt;td&gt;$0.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pack price becomes zero&lt;/td&gt;
&lt;td&gt;$0.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yield becomes 0% or 101%&lt;/td&gt;
&lt;td&gt;Reject&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pack weight becomes zero&lt;/td&gt;
&lt;td&gt;Reject&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Input is &lt;code&gt;NaN&lt;/code&gt;, &lt;code&gt;Infinity&lt;/code&gt;, a string or a boolean&lt;/td&gt;
&lt;td&gt;Reject&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Before publication, the sample function was executed against 14 assertions covering those cases and negative-input boundaries. That checks the teaching function; it does not certify a full product, an accounting system or a real kitchen's data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round for display, not at every intermediate step
&lt;/h2&gt;

&lt;p&gt;Keep the unrounded ingredient values when calculating a batch total and portion cost. Formatting every intermediate value to two decimals can accumulate rounding differences across many ingredients.&lt;/p&gt;

&lt;p&gt;JavaScript &lt;code&gt;Number&lt;/code&gt; is binary floating point. For an estimate displayed in a browser, a formatter can present the result in the selected currency. A payment ledger or another workflow requiring exact settlement needs its own decimal and rounding rules; changing the display formatter does not create accounting precision.&lt;/p&gt;

&lt;p&gt;Ingredient cost also is not profit. Labor, overhead, waste assumptions, taxes and payment fees belong in separately defined inputs and outputs. Calling a number “profit” when it only subtracts ingredients is a labeling bug even when the subtraction is correct.&lt;/p&gt;

&lt;h2&gt;
  
  
  A browser worksheet to explore
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kevanto.net/" rel="noopener noreferrer"&gt;Kevanto&lt;/a&gt; provides a free recipe-cost worksheet, including yield inputs, batch scaling, local recipe storage and CSV/JSON exports. The &lt;a href="https://kevanto.net/costing-worksheet" rel="noopener noreferrer"&gt;blank and worked costing worksheet&lt;/a&gt; can also be used to follow a calculation on paper. This account is associated with the project; these links are a product disclosure, not an independent review.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI disclosure:&lt;/strong&gt; This article and its illustrative code were generated by an AI assistant for the product owner. The sample checks were executed before publication. No firsthand restaurant operating experience, customer outcomes or measured savings are claimed.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Five calculation and PDF pitfalls in a browser invoice tool</title>
      <dc:creator>lei zhang</dc:creator>
      <pubDate>Sat, 24 Oct 2026 04:05:02 +0000</pubDate>
      <link>https://dev.to/lei_zhang_b6e446ccf73eac3/five-calculation-and-pdf-pitfalls-in-a-browser-invoice-tool-46j4</link>
      <guid>https://dev.to/lei_zhang_b6e446ccf73eac3/five-calculation-and-pdf-pitfalls-in-a-browser-invoice-tool-46j4</guid>
      <description>&lt;p&gt;I make &lt;a href="https://billpad.cc/" rel="noopener noreferrer"&gt;Billpad&lt;/a&gt;, a browser-based tool for creating invoices, estimates, receipts and proforma invoices. It does not collect payments or send invoice emails. Working on a document tool brings up a smaller engineering problem that is easy to underestimate: the number in the editor must match the number in the downloaded document.&lt;/p&gt;

&lt;p&gt;Here is a practical way to reason about that problem. The examples below are synthetic and describe calculation behavior, rather than tax rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Pick the rounding boundary before formatting the output
&lt;/h2&gt;

&lt;p&gt;Suppose a document has three lines:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Line&lt;/th&gt;
&lt;th&gt;Quantity&lt;/th&gt;
&lt;th&gt;Unit price&lt;/th&gt;
&lt;th&gt;Tax rate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Design work&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1200.00 USD&lt;/td&gt;
&lt;td&gt;8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Template set&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;75.00 USD&lt;/td&gt;
&lt;td&gt;8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Workshop&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;95.00 USD&lt;/td&gt;
&lt;td&gt;0%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The subtotal is 2030.00 USD. The taxable base in this example is 1650.00 USD, so the tax is 132.00 USD and the total is 2162.00 USD.&lt;/p&gt;

&lt;p&gt;A tempting implementation is to format each visible number independently, then recalculate the PDF total from the original floating-point inputs. That creates two separate paths through rounding. A better boundary is to create a calculation result once and make the editor, print view and PDF renderer consume it.&lt;/p&gt;

&lt;p&gt;Formatting should turn a result into text. It should not decide the financial result.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Minor units depend on the selected currency
&lt;/h2&gt;

&lt;p&gt;“Store money as cents” is incomplete advice. A document generator can support currencies with zero, two or three fractional digits. The integer 1234 means different major-unit amounts depending on the currency metadata.&lt;/p&gt;

&lt;p&gt;Keep the currency code and its precision with the calculation. Treat a currency change explicitly: relabeling an amount is not an exchange-rate conversion. Billpad offers currency selection, but it does not perform foreign-exchange conversion.&lt;/p&gt;

&lt;p&gt;Integer minor units simplify downstream additions and comparisons, but converting a decimal input into those units still needs a deliberate rounding rule. Binary floating-point input, unusually precise unit prices and large values all deserve attention. For a larger billing system, an explicit decimal arithmetic library or a carefully bounded decimal parser may be preferable.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A blank tax override is different from zero
&lt;/h2&gt;

&lt;p&gt;In the table above, the workshop's zero rate is intentional. It must not fall back to the document's default 8% rate.&lt;/p&gt;

&lt;p&gt;This condition loses that distinction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;appliedRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;defaultRate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the data model uses &lt;code&gt;null&lt;/code&gt; for “inherit the default,” the decision can be explicit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;appliedRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
  &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;defaultRate&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parser needs the same distinction. An empty input, an explicit zero and an invalid string are three different states. Converting all three into zero makes a broken or incomplete document look valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Discounts introduce an allocation problem
&lt;/h2&gt;

&lt;p&gt;With multiple tax rates, a document-level discount cannot simply disappear from the taxable base. It needs an allocation policy.&lt;/p&gt;

&lt;p&gt;For example, a 10% discount on this synthetic subtotal is 203.00 USD. Proportional allocation reduces the 8% group's base from 1650.00 to 1485.00 USD, and the zero-rate group's base from 380.00 to 342.00 USD. The example's tax becomes 118.80 USD and the total becomes 1945.80 USD.&lt;/p&gt;

&lt;p&gt;A fixed discount can produce fractional minor-unit allocations. Decide where any remainder goes, keep the allocation deterministic, and check that allocated discounts add back to the original discount. The correct policy for a regulated invoicing system may also depend on its jurisdiction and accounting requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Keep calculation checks separate from PDF appearance checks
&lt;/h2&gt;

&lt;p&gt;Useful calculation checks include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A line's explicit zero rate overrides a nonzero default.&lt;/li&gt;
&lt;li&gt;Allocated discounts sum to the document discount.&lt;/li&gt;
&lt;li&gt;Adding a payment received changes the balance, not the original line prices.&lt;/li&gt;
&lt;li&gt;Zero- and three-decimal currencies use their selected precision.&lt;/li&gt;
&lt;li&gt;Empty and invalid inputs remain distinguishable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PDF inspection answers different questions: does a long description wrap, do page breaks retain the totals, and can the font represent the entered text? A correct calculation does not prove that the PDF displays it correctly. Conversely, a polished screenshot does not prove the arithmetic.&lt;/p&gt;

&lt;p&gt;Billpad uses a text-based PDF path for supported characters and can fall back to a raster representation for others. That is a useful limitation to state explicitly; “PDF export” does not imply every document has selectable text.&lt;/p&gt;

&lt;p&gt;If you have built similar browser tools, I would be interested in the rounding or layout edge case that caused the most trouble. This article was generated by an AI assistant using Billpad's calculation code and documentation. The worked examples were checked against the calculation code.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
