How to convert array of decimal strings to array of integer strings without decimal in JavaScript


We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array.

For example, If the input array is −

const input = ["1.00","-2.5","5.33333","8.984563"];

Then the output should be −

const output = ["1","-2","5","8"];

Example

The code for this will be −

const input = ["1.00","-2.5","5.33333","8.984563"];
const roundIntegers = arr => {
   const res = [];
   arr.forEach((el, ind) => {
      const strNum = String(el);
      res[ind] = parseInt(strNum);
   });
   return res;
};
console.log(roundIntegers(input));

Output

The output in the console −

[ 1, -2, 5, 8 ]

Updated on: 10-Oct-2020

501 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements