What changes can be made to the function below to ensure it reads up to 3 decimal

I am utilizing a javascript function called tafgeet to convert numbers into Arabic words. However, the issue is that it only supports up to 2 decimal places. How can I adjust the function to handle 3 decimal places?

Currently, it translates numbers up to a million on the left side, so it should be feasible to apply a similar process on the right side.

I attempted to tweak the function but without success. Below is the original javascript code:

/**
 * TafgeetJS module.
 * @module TafgeetJS
 * @description Converts currency digits into written Arabic words
 * @author Mohammed Mahgoub <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d30303c353a32283f1d3a303c3431733e3230">[email protected]</a>>
 */

function Tafgeet(digit) {
  var currency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "SDG";

  //Split fractions
  var splitted = digit.toString().split(".");
  this.fraction = 0;
  if (splitted.length > 1) {
    var fraction = parseInt(splitted[1]);
    if (fraction >= 1 && fraction <= 99) {
      this.fraction =  splitted[1].length === 1 ? fraction * 10 : fraction;
    } else {
      //trim it
      var trimmed = Array.from(splitted[1]);
      this.fraction = "" + trimmed[0] + trimmed[1];
    }
  }
  this.digit = splitted[0];
  this.currency = currency;
}

Tafgeet.prototype.parse = function () {
  var serialized = [];
  var tmp = [];
  var inc = 1;
  var count = this.length();
  var column = this.getColumnIndex();
  if (count >= 16) {
    console.error("Number out of range!");
    return;
  }
  //Separate the number into columns
  Array.from(this.digit.toString()).reverse().forEach(function (d, i) {
    tmp.push(d);
    if (inc == 3) {
      serialized.unshift(tmp);
      tmp = [];
      inc = 0;
    }
    if (inc == 0 && count - (i + 1) < 3 && count - (i + 1) != 0) {
      serialized.unshift(tmp);
    }
    inc++;
  });

// Rest of the JavaScript code...
  
module.exports = Tafgeet;

Thank you.

Answer №1

Transform the following code:

this.fraction = "" + trimmed[0] + trimmed[1];

Into this:

this.fraction = "" + trimmed[0] + trimmed[1] + trimmed[2];

Keep in mind that this solution is basic and requires further validations and checks before implementation in a production environment.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Animation not fluid with ReactCSSTransitionGroup

Currently, I am in the process of developing an image carousel that showcases images moving smoothly from right to left. However, despite its functionality, there seems to be a slight jaggedness in the animation that I can't seem to resolve. Interesti ...

Adding an item to a collection using NgRx

I am working with a state that has a specific structure. It consists of a list of Workouts, and each Workout contains a list of exercises associated with it. I have two main objectives: To add new exercises to a particular workout from the existing list o ...

The timing calculations in Vue.js do not align with the standard JavaScript version

I am currently working on developing a 'beats per minute' (BPM) calculator, which is similar to the one available here. However, I have noticed that when using the BPM calculator from that link for testing on a particular song, it quickly approxi ...

How can you access additional fields beyond what is displayed in a dropdown select menu in React?

I am working with an array of Jsons that contain the fields ID, name, and description. My goal is to create a dropdown selection box that will show both the name and description when clicked, and then store the associated ID in the rawID state. I have been ...

Can AngularJS be integrated with prototype methods and variables?

When working with AngularJS, the majority of logic is typically based on the $scope: function Ctrl($scope) { $scope.name = "Freewind"; $scope.hello = function() { alert($scope.name); } $scope.method1 = function() {} $scope.metho ...

The socket.io client in my JavaScript code is failing to receive the necessary event

Currently, I am in the process of configuring a socket.io chat feature with an expressjs backend and sveltejs frontend. I have established a custom namespace named 'chat' where a new room is generated upon a 'join' request. My appro ...

Utilizing Font Awesome icons dynamically presents challenges when integrating with SVG & JavaScript

Recently, I started using the new JS&SVG implementation of font-awesome's v5 icons. It seems to be working perfectly for icons (such as <i class='fas fa-home'></i>) that are already present in the DOM at page load. The <i& ...

A guide on extracting the values of checked checkboxes by their ID using javascript

I'm currently attempting to extract the values of selected checkboxes. These checkboxes have distinct IDs because they are specified within a modal window. <input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = &a ...

Dynamic parameters can be passed in Rails using the link_to method

Feeling a bit stuck, maybe this is just a simple question. I have a sort button that I need to use to organize my list of questions. To do this, I create a link_to like this: <%= link_to xx_path(:is_sort => true, :remote=> true , :method=> :p ...

Canvas - Drawing restricted to new tiles when hovered over, not the entire canvas

Imagine having a canvas divided into a 15x10 32-pixel checkerboard grid. This setup looks like: var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var tileSize = 32; var xCoord var yCoord ...

The JSON data parser does not support the use of single quotation marks

Using PHP, I am storing user "comments" from my website in a database and escaping special characters with mysql_real_escape_string(). This helps to avoid any issues with single quotes (') or double quotes ("). To display these comments on the website ...

How do you modify the up vector of the camera in three.js?

When working with three.js, I encountered a situation where I set the camera's focal point using camera.lookAt(0, 0, 0), but the camera's up vector ended up pointing in a random direction. I have a specific up vector in mind that I want the camer ...

Display or conceal a <div> segment based on the drop down selection made

A dropdown menu controls the visibility of certain div elements based on the selection made. While this functionality is working for one dropdown, it's not working for another even though the code is very similar. I've tried various solutions but ...

Unable to administer AuthenticationController

I am attempting to inject a controller into my app.run function, but I keep encountering the following error: Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.2.10/$injector/unpr?p0=AuthenticationControllerProvider%20%3C-%20AuthenticationCon ...

Save the text found within an element to Appim wd.js

I am a beginner with Appium and I am currently exploring the workshop Git project, which utilizes wd.js and Grunt. From my research in the documentation and forums, I have learned that there are two methods for accessing the text of native elements within ...

Controller experiencing issues with Ajax passing null value

My webpage features a dropdown menu with a list of ID's to choose from. When a customer selects an option, it should update the price total displayed on the page. To achieve this functionality, I'm working on implementing an AJAX call that will u ...

Adjust the canvas size to fit its parent element ion-grid

I am currently working on an Ionic 3 component that displays a grid of images connected by a canvas overlay. I have utilized Ionic's ion-grid, but I am facing an issue with resizing the canvas. The problem is that I cannot determine the correct dimens ...

Having trouble with PHP's json_decode function with GET variables in an object?

I am currently working with an angular code that utilizes jsonp. One issue I am encountering is related to the object variable 'o_params' in my params. Here is the javascript code snippet: $http({ method: 'JSONP', ...

useEffect initiates all actions

I'm currently exploring hooks functionality within a Next.JS project. I've successfully used a useEffect to track scrolling behavior in order to dynamically change the content displayed in a header when the page is scrolled. const [ scrollY, setS ...

Tally the quantity of data points within jQuery Datatables

Upon navigating to my jQuery DataTable, I aim to showcase the count of Users pending activation. Typically, I would use fnGetData with (this), but since I am not triggering this on a click event and wish to count all entries in the table, I am unsure of ho ...