Answers for "js remove leading zeros"

3

javascript remove leading zeros from string

use a regular expression like this  

var strNumber = "0049";  
console.log(typeof strNumber); //string  
  
var number  = strNumber.replace(/^0+/, '');
Posted by: Guest on March-15-2021
0

js remove zeros after decimal

(+n).toFixed(2).replace(/(\.0+|0+)$/, '')


// 0 => 0
// 0.1234 => 0.12
// 0.1001 => 0.1

// 1 => 1
// 1.1234 => 1.12
// 1.1001 => 1.1

// 100 => 100
// 100.1234 => 100.12
// 100.1001 => 100.1
Posted by: Guest on April-13-2021
0

remove trailing zeros javascript

var n = 6.96900
var noTrailingZeroes = n.toString()
console.log(noTrailingZeroes) // -> 6.969
Posted by: Guest on May-24-2021
0

remove leading characters in javascript

1. Using substring()
The substring() method returns the part of the string between the specified indexes, or to the end of the string.

let str = 'Hello';
 
str = str.substring(1);
console.log(str);
 
/*
    Output: ello
*/
 
The solution can be easily extended to remove first n characters from the string.


let str = 'Hello';
let n = 3;
 
str = str.substring(n);
console.log(str);
 
/*
    Output: lo
*/
 
___________________________________________________________________________
2. Using slice()
The slice() method extracts the text from a string and returns a new string.

let str = 'Hello';
 
str = str.slice(1);
console.log(str);
 
/*
    Output: ello
*/
 
This can be easily extended to remove first n characters from the string.


let str = 'Hello';
let n = 3;
 
str = str.slice(n);
console.log(str);
 
/*
    Output: lo
*/
 
__________________________________________________________________________
3. Using substr()
The substr() method returns a portion of the string, starting at the specified index and extending for a given number of characters or till the end of the string.


let str = 'Hello';
 
str = str.substr(1);
console.log(str);
 
/*
    Output: ello
*/
 
Note that substr() might get deprecated in future and should be avoided.
Posted by: Guest on April-03-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language