function toFixed(num, fixed) {
    var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
    return num.toString().match(re)[0];
}

Example

toFixed(1.05, 1) //1.0
toFixed(1.05, 0) //1
Last modified: 18/10/2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.