How to replace all occurrences of a specific digit in a floating-point number with JavaScript

1 Answer

0 votes
function replaceAllDigits(num, oldDigit, newDigit) {
    let str = num.toString();
    let regex = new RegExp(oldDigit, 'g');

    str = str.replace(regex, newDigit);
    
    return parseFloat(str);
}

console.log(replaceAllDigits(82420.291, 2, 6)); 
console.log(replaceAllDigits(111.11, 1, 5)); 



/*
run:
  
86460.691
555.55
  
*/

 



answered 1 day ago by avibootz
edited 1 day ago by avibootz

Related questions

...