0

How to remove the last decimal point of .36.00 which is the .00 without affecting the other data that have .00 like 28.003 and 2.0003

here's the code: https://stackblitz.com/edit/ng-zorro-antd-start-8t6pdr?file=src/app/app.component.ts

const s= ['.37.00 Mbit/s', '32.023 Mbit/s', '23.283 Mbit/s', '28.003 Mbit/s', '2.0003 Mbit/s'];
   for (let x = 0; x <= s.length; x++) {
     const sam = s[x].replace(/[^0-9+.]/g, "");
     console.log(sam)
   }

expected output.

   .37
    32.023
    23.283 
28.003
2.0003
BCMDKX
  • 11
  • 6

3 Answers3

0

Try this.

const s = ['.37.00 Mbit/s', '32.023 Mbit/s', '23.283 Mbit/s', '28.003 Mbit/s', '2.0003 Mbit/s'];
for (let x = 0; x < s.length; x++) {
    let sam = s[x].replace(/[a-zA-z/\s]/g, "").replace(/(\.00)$/g, "")
    console.log(sam);
}
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
0

.37.00 Mbit/s looks like a broken format, there should never be more than one decimal character in a number.

I would do this, but without knowing all details I don't know if it is working for you:

    /* (https://stackoverflow.com/a/14480366/1479486 by Denys Séguret) */
    function getNthPosition(string, subString, index) {
       return string.split(subString, index).join(subString).length;
    }

    const s= ['.37.00 Mbit/s', '32.023 Mbit/s', '23.283 Mbit/s', '28.003 Mbit/s', '2.0003 Mbit/s'];
   for (let x = 0; x <= s.length; x++) {
     const sam = s[x].substring(0, getNthPosition(s[x], '.', 2)-1);
     console.log(sam)
   }

This will remove everything after the second '.', including the dot

Tim Rasim
  • 655
  • 7
  • 20
0

Using ES6, Format will be using

const format = (num) => {
    const [num1, num2] = String(num).split(".")
    if (!num2) return num1
    else return `${num1}.${num2}`
}
const s = ['.37.00 Mbit/s', '32.023 Mbit/s', '23.283 Mbit/s', '28.003 Mbit/s', '2.0003 Mbit/s'];
s.forEach(x => {
    console.log(format(x.replace(/ .+/, "")))
})
xdeepakv
  • 7,835
  • 2
  • 22
  • 32