Remove leading zeros from a number in Javascript
JavascriptHtmlParseintJavascript Problem Overview
> Possible Duplicate:
> Truncate leading zeros of a string in Javascript
What is the simplest and cross-browser compatible way to remove leading zeros from a number in Javascript ?
e.g. If I have a textbox value as 014 or 065, it should only return 14 or 65
Javascript Solutions
Solution 1 - Javascript
We can use four methods for this conversion
- [parseInt][1] with [radix][4]
10
- [Number Constructor][2]
- [Unary Plus Operator][3]
- Using mathematical functions (subtraction)
const numString = "065";
//parseInt with radix=10
let number = parseInt(numString, 10);
console.log(number);
// Number constructor
number = Number(numString);
console.log(number);
// unary plus operator
number = +numString;
console.log(number);
// conversion using mathematical function (subtraction)
number = numString - 0;
console.log(number);
Update(based on comments): Why doesn't this work on "large numbers"?
For the primitive type Number
, the safest max value is 253-1([Number.MAX_SAFE_INTEGER
][5]).
console.log(Number.MAX_SAFE_INTEGER);
Now, lets consider the number string '099999999999999999999' and try to convert it using the above methods
const numString = '099999999999999999999';
let parsedNumber = parseInt(numString, 10);
console.log(`parseInt(radix=10) result: ${parsedNumber}`);
parsedNumber = Number(numString);
console.log(`Number conversion result: ${parsedNumber}`);
parsedNumber = +numString;
console.log(`Appending Unary plus operator result: ${parsedNumber}`);
parsedNumber = numString - 0;
console.log(`Subtracting zero conversion result: ${parsedNumber}`);
All results will be incorrect.
That's because, when converted, the numString value is greater than Number.MAX_SAFE_INTEGER
. i.e.,
99999999999999999999 > 9007199254740991
This means all operation performed with the assumption that the string
can be converted to number
type fails.
For numbers greater than 253, primitive [BigInt
][6] has been added recently. Check browser compatibility of BigInt
[here][7].
The conversion code will be like this.
const numString = '099999999999999999999';
const number = BigInt(numString);
###P.S: Why radix is important for parseInt
?
If radix is undefined or 0 (or absent), JavaScript assumes the following:
- If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed
- If the input string begins with "0", radix is eight (octal) or 10 (decimal)
- If the input string begins with any other value, the radix is 10 (decimal)
Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.
For this reason, always specify a radix when using parseInt [1]: https://developer.mozilla.org/en/Javascript/Reference/Global_Objects/parseInt [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#Constructor [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus [4]: http://en.wikipedia.org/wiki/Radix [5]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER [6]: https://tc39.es/ecma262/#sec-bigint-objects [7]: https://caniuse.com/#feat=bigint
Solution 2 - Javascript
regexp:
"014".replace(/^0+/, '')
Solution 3 - Javascript
It is not clear why you want to do this. If you want to get the correct numerical value, you could use unary +
[docs]:
value = +value;
If you just want to format the text, then regex could be better. It depends on the values you are dealing with I'd say. If you only have integers, then
input.value = +input.value;
is fine as well. Of course it also works for float values, but depending on how many digits you have after the point, converting it to a number and back to a string could (at least for displaying) remove some.