<?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: Yahya LAZREK</title>
    <description>The latest articles on DEV Community by Yahya LAZREK (@yahyalazrek).</description>
    <link>https://dev.to/yahyalazrek</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3812983%2F43dcbe63-c9e6-48f0-b9ce-5067eed846c2.jpeg</url>
      <title>DEV Community: Yahya LAZREK</title>
      <link>https://dev.to/yahyalazrek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yahyalazrek"/>
    <language>en</language>
    <item>
      <title>Why Casting Double to Decimal Won't Save Your Financial Data 💸 (A Bug Hunt Story)</title>
      <dc:creator>Yahya LAZREK</dc:creator>
      <pubDate>Wed, 11 Mar 2026 14:08:58 +0000</pubDate>
      <link>https://dev.to/yahyalazrek/why-casting-double-to-decimal-wont-save-your-financial-data-a-bug-hunt-story-332c</link>
      <guid>https://dev.to/yahyalazrek/why-casting-double-to-decimal-wont-save-your-financial-data-a-bug-hunt-story-332c</guid>
      <description>&lt;p&gt;Today at work, I was deep in the trenches of a large legacy codebase, tracking down a weird bug. The application handles a lot of financial data, and at a certain point in the logic, an account balance was supposed to be exactly &lt;code&gt;0&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Instead, the system was throwing errors because the value was something like &lt;code&gt;0.000000000000000014&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Ah, yes. The dreaded &lt;strong&gt;floating-point noise&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When I dug into the source code, I found the culprit. A previous developer had used a &lt;code&gt;double&lt;/code&gt; to calculate the financial values, and then, right at the end, did something like this to "fix" it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;finalBalance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;calculatedDoubleBalance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spoiler alert: &lt;strong&gt;This does not work.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Here is why casting a &lt;code&gt;Double&lt;/code&gt; to a &lt;code&gt;Decimal&lt;/code&gt; won't save you, and why you need to understand the difference between the two when dealing with money.&lt;/p&gt;




&lt;h3&gt;
  
  
  🥊 Double vs. Decimal: What’s the difference?
&lt;/h3&gt;

&lt;p&gt;To understand the bug, we have to look at how computers store numbers under the hood.&lt;/p&gt;

&lt;h4&gt;
  
  
  The &lt;code&gt;Double&lt;/code&gt; (Floating-Point)
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;double&lt;/code&gt; (short for double-precision floating-point) represents numbers using &lt;strong&gt;Base-2 (binary) fractions&lt;/strong&gt;. &lt;br&gt;
Because it uses binary, it can't perfectly represent all Base-10 (decimal) fractions. Just like &lt;code&gt;1/3&lt;/code&gt; results in &lt;code&gt;0.333333...&lt;/code&gt; in our normal math, fractions like &lt;code&gt;0.1&lt;/code&gt; result in an endlessly repeating binary fraction for a computer. &lt;/p&gt;

&lt;p&gt;To make it fit into memory, the computer cuts it off, resulting in a tiny loss of precision. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Blazing fast, uses less memory, and can store astronomically large or microscopic numbers. Perfect for physics, graphics, and scientific calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; &lt;code&gt;0.1 + 0.2&lt;/code&gt; equals &lt;code&gt;0.30000000000000004&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  The &lt;code&gt;Decimal&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;decimal&lt;/code&gt; represents numbers using &lt;strong&gt;Base-10 math&lt;/strong&gt;. It was specifically created to handle money and financial calculations. It stores exact decimal fractions. If you tell it to store &lt;code&gt;0.1&lt;/code&gt;, it stores exactly &lt;code&gt;0.1&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; 100% precision for decimal numbers. No floating-point noise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Slower to compute and takes up more memory (usually 128-bit vs a double's 64-bit).&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  🔍 Why the cast failed
&lt;/h3&gt;

&lt;p&gt;In the bug I found today, the developer knew that the final output needed to be a &lt;code&gt;Decimal&lt;/code&gt;. But because the math &lt;em&gt;leading up to that point&lt;/em&gt; was done using &lt;code&gt;Doubles&lt;/code&gt;, the precision was already lost.&lt;/p&gt;

&lt;p&gt;Casting a corrupted &lt;code&gt;Double&lt;/code&gt; into a &lt;code&gt;Decimal&lt;/code&gt; is like taking a blurry, low-resolution photo and saving it as a massive 4K PNG. &lt;strong&gt;It doesn't magically restore the lost details; it just gives you a very high-resolution, blurry photo.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you cast a &lt;code&gt;double&lt;/code&gt; with floating-point noise to a &lt;code&gt;decimal&lt;/code&gt;, the &lt;code&gt;decimal&lt;/code&gt; faithfully records that exact noise.&lt;/p&gt;
&lt;h4&gt;
  
  
  Let's see it in action (C# example):
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;deposit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;anotherDeposit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;doubleBalance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deposit&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;anotherDeposit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// doubleBalance is now 0.30000000000000004&lt;/span&gt;

&lt;span class="c1"&gt;// Let's try to "fix" it by casting!&lt;/span&gt;
&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;decimalBalance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;doubleBalance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimalBalance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Output: 0.30000000000000004 ❌ We just preserved the error!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Because the math was already executed as a &lt;code&gt;double&lt;/code&gt;, the damage was done. When subtracting values later down the line, instead of hitting exactly &lt;code&gt;0&lt;/code&gt;, the system was left with microscopic pennies, breaking the business logic.&lt;/p&gt;


&lt;h3&gt;
  
  
  🛠️ How to handle this properly
&lt;/h3&gt;

&lt;p&gt;If you are working with money, currencies, or any system where exact Base-10 precision is required, follow these rules:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. NEVER use Double or Float for money.&lt;/strong&gt; &lt;br&gt;
Not in your database, not in your API payloads, and not in your code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Use Decimal from start to finish.&lt;/strong&gt;&lt;br&gt;
Declare your variables as &lt;code&gt;decimal&lt;/code&gt; right away.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;deposit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.1m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;anotherDeposit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.2m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;decimalBalance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deposit&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;anotherDeposit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimalBalance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Output: 0.3 ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. What if you &lt;em&gt;have&lt;/em&gt; to deal with a legacy Double?&lt;/strong&gt;&lt;br&gt;
If you are consuming a legacy API or database that gives you a &lt;code&gt;double&lt;/code&gt;, and you need to convert it to a &lt;code&gt;decimal&lt;/code&gt; to do financial math, you need to use &lt;strong&gt;rounding&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;legacyValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.30000000000000004&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Round it to the precision of your currency (e.g., 2 decimal places) &lt;/span&gt;
&lt;span class="c1"&gt;// BEFORE or DURING the transition to Decimal.&lt;/span&gt;
&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;cleanDecimal&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Round&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;legacyValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cleanDecimal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 0.30 ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Finding this bug today was a great reminder that types matter. &lt;code&gt;Double&lt;/code&gt; is great for calculating the trajectory of a rocket, but it's terrible for calculating someone's paycheck. &lt;/p&gt;

&lt;p&gt;Have you ever spent hours chasing down a floating-point bug in your codebase? Let me know in the comments! 👇&lt;/p&gt;

</description>
      <category>programming</category>
      <category>debugging</category>
      <category>csharp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Stop relying on Regex for Email Validation (Here is a drop-in React component)</title>
      <dc:creator>Yahya LAZREK</dc:creator>
      <pubDate>Sun, 08 Mar 2026 14:29:32 +0000</pubDate>
      <link>https://dev.to/yahyalazrek/stop-relying-on-regex-for-email-validation-here-is-a-drop-in-react-component-3mdl</link>
      <guid>https://dev.to/yahyalazrek/stop-relying-on-regex-for-email-validation-here-is-a-drop-in-react-component-3mdl</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp8232mu05tm6s5bnb1ou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp8232mu05tm6s5bnb1ou.png" alt="Vector graphic showing an email validation process where a funnel successfully removes fake accounts and bots, leaving only verified user profiles." width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are building a SaaS, you know the pain of fake signups. &lt;/p&gt;

&lt;p&gt;Standard email regex (&lt;code&gt;^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$&lt;/code&gt;) is no longer enough. It passes &lt;code&gt;temp-mail.org&lt;/code&gt;, &lt;code&gt;yopmail&lt;/code&gt;, and fat-finger typos like &lt;code&gt;user@gmil.com&lt;/code&gt;. This leads to a database full of garbage and high bounce rates that ruin your email deliverability.&lt;/p&gt;

&lt;p&gt;I only have a couple of hours a day to work on my side projects, so I got tired of dealing with this. I spent the weekend building a "Smart" Email Input component in React that actually does deep validation.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it does differently:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Live DNS/MX Check:&lt;/strong&gt; It pings the domain to ensure it actually has active mail servers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Burner Block:&lt;/strong&gt; It checks against a constantly updated list of disposable email providers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typosquatting Autocorrect:&lt;/strong&gt; It detects typos on major providers and prompts the user (e.g., &lt;em&gt;"Did you mean &lt;a href="mailto:user@gmail.com"&gt;user@gmail.com&lt;/a&gt;?"&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Saves API calls:&lt;/strong&gt; It only triggers on &lt;code&gt;onBlur&lt;/code&gt; (when the user clicks out of the input).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Code (Open Source)
&lt;/h3&gt;

&lt;p&gt;I styled it with Tailwind, so it's super easy to drop into your Next.js or React forms. &lt;/p&gt;

&lt;p&gt;You can grab the full &lt;code&gt;SmartEmailInput.jsx&lt;/code&gt; file from my GitHub repository here:&lt;br&gt;
👉 &lt;a href="https://github.com/yahyalazrek/react-smart-email-input" rel="noopener noreferrer"&gt;https://github.com/yahyalazrek/react-smart-email-input&lt;/a&gt;&lt;br&gt;
🌐 Website: &lt;a href="https://emailguard.lazrek.net" rel="noopener noreferrer"&gt;https://emailguard.lazrek.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzdcrmjqp29egs8i8vmq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzdcrmjqp29egs8i8vmq.png" alt="Email verification form showing real-time error messages blocking disposable, invalid, and inactive email addresses." width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: The logic runs on an edge-optimized Cloudflare Worker I deployed to RapidAPI. The GitHub Readme has a link to grab a free API key with 100 requests/mo so you can test the component instantly).&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you handle validation?
&lt;/h3&gt;

&lt;p&gt;Let me know what you think of the code! Do you guys use double opt-in, or do you block fake domains at the form level? Would love to hear how other devs are handling this!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
