0
"07999323384"

How do I convert the number above to number, but once I use any numeric method it takes out the first zero

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 3
    that is not possible. leading zeroes carry no value. that's why they are omitted – jo3rn Feb 19 '21 at 13:01
  • Isn't there some canonical dupe target for this? It's the 237538475th time someone asks this. It still doesn't make any sense at all. As numbers, 0x01, 0001, 1, 0b1, 1e0, 1.0000, are completely identical. There is no "taking out the first zero", or "keep trailing zeroes". As numbers, they are the exact same thing. The notation may be different, but a notation isn't a number. – ASDFGerte Feb 19 '21 at 13:07
  • 1
    This is an Iraqi phone number as I see... you can put more description to illustrate why you need to convert it to a number, if you want to add +964 to this string you don't need to do it that way. – Ali Abbas Feb 19 '21 at 13:11
  • What are you trying to do with this value? – Felix Kling Feb 19 '21 at 13:11
  • If it is a phone number, you should not convert it to a number type. Either keep it as a string, or use a phone number handling library to normalize and validate. https://www.npmjs.com/search?q=phone – Håken Lid Feb 19 '21 at 13:31
  • You should not think of a phone number as numeric data. It is a string of digit characters. Numeric types are useful for mathematical calculations. There's never any reason to perform arithmetic on phone numbers. (It would be neat if you could multiply two phone numbers to create a group call, but that's not possible) – Håken Lid Feb 19 '21 at 13:38

2 Answers2

2

You use Number("07999323384"); except it will take out the first 0, its not possible to have a number beginning with 0 (except the number 0) because js removes leading 0s.

There is nothing you can do about this however that shouldnt matter because there should be no situation where you need it as a number type but also need the first 0

Luke_
  • 745
  • 10
  • 23
1

Create a copy in a seperate variable to do calculations on (which will drop the leading zero) and use the original variable to maintain the leading zero, it's either that or pad out the number after using your numeric methods presuming it's a fixed length as per this: How can I pad a value with leading zeros?

mighty mac
  • 19
  • 3