<?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: ADEYINKA</title>
    <description>The latest articles on DEV Community by ADEYINKA (@_incrediblemind).</description>
    <link>https://dev.to/_incrediblemind</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%2F445494%2F219044b1-f4bc-4b4e-bb1d-ff3d841f99d0.jpg</url>
      <title>DEV Community: ADEYINKA</title>
      <link>https://dev.to/_incrediblemind</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_incrediblemind"/>
    <language>en</language>
    <item>
      <title>Implementing a Custom Load More Button in Angular Angular</title>
      <dc:creator>ADEYINKA</dc:creator>
      <pubDate>Mon, 02 Dec 2024 11:42:00 +0000</pubDate>
      <link>https://dev.to/_incrediblemind/implementing-a-custom-load-more-button-in-angular-angular-2n0b</link>
      <guid>https://dev.to/_incrediblemind/implementing-a-custom-load-more-button-in-angular-angular-2n0b</guid>
      <description>&lt;p&gt;Pagination is still the most popular way to load new items on a website because it ships by default in almost every single e-commerce platform. However, usability tests sessions found “Load more” buttons combined with lazy-loading to be a superior implementation, resulting in a more seamless user experience. We found that infinite scrolling can be downright harmful to usability — in particular, for search results and on mobile. However, it’s not black and white, because the performance of each method varies according to the context of the page.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Formatting Numbers to Words with Commas Using Angular Pipes</title>
      <dc:creator>ADEYINKA</dc:creator>
      <pubDate>Sun, 02 Jul 2023 16:11:28 +0000</pubDate>
      <link>https://dev.to/_incrediblemind/formatting-numbers-to-words-with-commas-using-angular-pipes-2gch</link>
      <guid>https://dev.to/_incrediblemind/formatting-numbers-to-words-with-commas-using-angular-pipes-2gch</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;br&gt;
In Angular applications, we often encounter scenarios where we need to format numbers as words. Additionally, adding commas to large numbers enhances readability. In this blog post, we will explore how to create an Angular pipe that converts numbers to words and adds commas at the appropriate places.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Creating the Angular Pipe&lt;/strong&gt;&lt;br&gt;
To get started, let's create an Angular pipe called &lt;em&gt;&lt;strong&gt;numberToWords&lt;/strong&gt;&lt;/em&gt;. This pipe will be responsible for transforming the input number into its corresponding words with commas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'numberToWords'
})
export class NumberToWordsPipe implements PipeTransform {
  transform(value: any): string {
    if (value &amp;amp;&amp;amp; this.isInteger(value)) {
      return this.numToWordsWithCommas(value);
    }
    return value;
  }

  // Helper methods for number to words conversion and comma insertion
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code snippet above, we define the &lt;em&gt;&lt;strong&gt;NumberToWordsPipe **_class and implement the _&lt;/strong&gt;PipeTransform**&lt;/em&gt; interface. This interface requires us to define a transform method, which is the main transformation logic for the pipe.&lt;/p&gt;

&lt;p&gt;The transform method takes the input value and checks if it exists and is an integer using the &lt;em&gt;&lt;strong&gt;isInteger&lt;/strong&gt;&lt;/em&gt; method. If the value is valid, it calls the &lt;em&gt;&lt;strong&gt;numToWordsWithCommas&lt;/strong&gt;&lt;/em&gt; method to convert the number to words with commas. If the value is not valid, it simply returns the original value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Implementing the Number to Words Conversion&lt;/strong&gt;&lt;br&gt;
To convert a given number into its corresponding words, we need to define helper methods within the &lt;em&gt;"NumberToWordsPipe"&lt;/em&gt; class.&lt;/p&gt;

&lt;p&gt;First, we implement the &lt;strong&gt;&lt;em&gt;numToWords&lt;/em&gt;&lt;/strong&gt; method, which takes a number and converts it into its corresponding words. The conversion logic involves breaking the number into chunks of three digits and converting each chunk to words using arrays of predefined words. Let's look at the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private numToWords(n: number): string {
    if (n === 0) {
      return 'zero';
    }
// Define arrays of words for numbers less than twenty, tens, and scales
    let lessThanTwenty = ['','one','two','three','four',   'five','six','seven','eight','nine','ten','eleven','twelve', 'thirteen','fourteen','fifteen','sixteen','seventeen', 'eighteen','nineteen'];

    let tens = ['','','twenty','thirty','forty','fifty','sixty',
      'seventy','eighty','ninety'];

    let scales = ['','thousand','million','billion',    'trillion'];

  // Define helper method convertChunk to convert individual chunks to words

    let convertChunk = (num: number): string =&amp;gt; {
      let chunkResult = '';

      let hundreds = Math.floor(num / 100);
      num %= 100;

      if (hundreds !== 0) {
        chunkResult += `${lessThanTwenty[hundreds]} hundred`;
      }

      if (num === 0) {
        return chunkResult;
      }

      if (chunkResult !== '') {
        chunkResult += ' and ';
      }

      if (num &amp;lt; 20) {
        chunkResult += lessThanTwenty[num];
      } else {
        let tensDigit = Math.floor(num / 10);
        let onesDigit = num % 10;
        chunkResult += `${tens[tensDigit]}${onesDigit !== 0 ? '-' + lessThanTwenty[onesDigit] : ''}`;
      }

      return chunkResult;
    };
  // Define helper method convertGroup to convert the entire number
    let convertGroup = (num: number, scale: string): string =&amp;gt; {
      let groupResult = '';
      let chunkCount = 0;

      while (num &amp;gt; 0) {
        let chunk = num % 1000;
        if (chunk !== 0) {
          let chunkWords = convertChunk(chunk);
          if (chunkCount &amp;gt; 0) {
            groupResult = `${chunkWords} ${scales[chunkCount]} ${groupResult}`;
          } else {
            groupResult = `${chunkWords} ${groupResult}`;
          }
        }
        num = Math.floor(num / 1000);
        chunkCount++;
      }

      return groupResult.trim();
    };

    return convertGroup(n, '');
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;&lt;strong&gt;numToWords&lt;/strong&gt;&lt;/em&gt; method, we define arrays &lt;strong&gt;&lt;em&gt;lessThanTwenty&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;tens&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;scales&lt;/em&gt;&lt;/strong&gt; that hold the words for numbers less than twenty, tens, and scales (thousand, million, billion, etc.) respectively. These arrays provide the foundation for converting the number to words.&lt;/p&gt;

&lt;p&gt;Next, we define the convertChunk method. This method takes a three-digit chunk of the number and converts it into words. The logic involves breaking the chunk into its hundreds, tens, and ones places, and then combining the corresponding words from the arrays defined earlier.&lt;/p&gt;

&lt;p&gt;To convert the entire number, we implement the &lt;strong&gt;&lt;em&gt;convertGroup&lt;/em&gt;&lt;/strong&gt; method. This method takes the original number and divides it into chunks of three digits. It iterates over each chunk, converts it to words using &lt;strong&gt;&lt;em&gt;convertChunk&lt;/em&gt;&lt;/strong&gt;, and appends the corresponding scale (thousand, million, billion, etc.) to each chunk. Finally, it returns the converted number as a string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Adding Commas to the Result&lt;/strong&gt;&lt;br&gt;
To enhance the readability of large numbers, we can add commas at the appropriate places. Let's implement the &lt;strong&gt;&lt;em&gt;addCommas&lt;/em&gt;&lt;/strong&gt; method within the &lt;strong&gt;&lt;em&gt;NumberToWordsPipe&lt;/em&gt;&lt;/strong&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private addCommas(str: string): string {
    let parts = str.split(' ');
    for (let i = 0; i &amp;lt; parts.length; i++) {
      if (parts[i] === 'and') {
        continue;
      }
      parts[i] = parts[i].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');

      if (parts[i] === 'thousand' || parts[i] === 'million' || parts[i] === 'billion' || parts[i] === 'trillion') {
        parts[i] = parts[i]+','
      }
    }
    return parts.join(' ');
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;&lt;strong&gt;addCommas&lt;/strong&gt;&lt;/em&gt; method, we split the converted string into an array of individual words using the space delimiter. We iterate over each word and use a regular expression to find any sequence of three digits (excluding the last group) and replace it with the same digits followed by a comma.&lt;/p&gt;

&lt;p&gt;We also provide special handling for words like 'thousand','million', 'billion',etc to ensure that a comma is added after them. This step is necessary because they act as  scale in the number-to-words conversion.&lt;/p&gt;

&lt;p&gt;Finally, we join the modified array of words back into a string and return it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Combining the Conversion and Comma Insertion&lt;/strong&gt;&lt;br&gt;
To obtain the final result of converting a number to words with commas, we combine the number-to-words conversion and comma insertion logic. We do this in the &lt;strong&gt;&lt;em&gt;numToWordsWithCommas&lt;/em&gt;&lt;/strong&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private numToWordsWithCommas(n: number): string {
  let result = this.numToWords(n);
  result = this.addCommas(result);

  return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;&lt;strong&gt;numToWordsWithCommas&lt;/strong&gt;&lt;/em&gt; method, we first call the &lt;em&gt;&lt;strong&gt;numToWords&lt;/strong&gt;&lt;/em&gt; method to convert the input number to words. We then pass the resulting string to the &lt;em&gt;&lt;strong&gt;addCommas&lt;/strong&gt;&lt;/em&gt; method to add commas at the appropriate places. Finally, we return the modified string as the final result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
In this blog post, we have learned how to create an Angular pipe that converts numbers to words and adds commas at the right places. By utilizing the &lt;em&gt;&lt;strong&gt;NumberToWordsPipe&lt;/strong&gt;&lt;/em&gt; in your Angular applications, you can easily format numbers to enhance readability and provide a better user experience.&lt;/p&gt;

&lt;p&gt;Feel free to modify and customize the code as per your specific requirements and use it in your Angular projects. This pipe can be especially useful in applications where numbers need to be displayed in a human-readable format, such as financial or data reporting systems.&lt;/p&gt;

&lt;p&gt;You can find the complete code and examples in the accompanying GitHub repository &lt;a href="https://github.com/yeanca/custom_pipe" rel="noopener noreferrer"&gt;https://github.com/yeanca/custom_pipe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this blog post has been informative and helpful. If you have any questions or feedback, please leave a comment below.&lt;/p&gt;

&lt;p&gt;Happy coding&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>angular</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
