5

I want to convert 1,2,3,..9,10,11,12...24 to 01,02,03,...09,10,11,12,...24. I tried these solutions. None of these solved my purpose:

  1. How to format numbers by prepending 0 to single-digit numbers?
  2. How to output numbers with leading zeros in JavaScript [duplicate]
  3. How can I pad a value with leading zeros?

I'm working on an Angular project and I'm doing this in Typescript. Here is my code:

monthpikcer.component.ts

monthSlice() {
    let monthStartingIndex = 0;
    monthStartingIndex = ("0" + monthStartingIndex).slice(-2); //error
    let monthEndingIndex = 24;
    for(monthStartingIndex =0; monthStartingIndex < monthEndingIndex; monthStartingIndex++) {
    ...
    } 
}

But for line monthStartingIndex = ("0" + monthStartingIndex).slice(-2), I'm getting an error:

Type 'string' is not assignable to type 'number'.

I want it to be 01, 02, 03...10,11,12,13...24. Please tell me how to do it without changing the data type of my variables because later I've to do some arithematics with them.

7
  • 1
    as far as i know most languages do not allow a number to start with 0 because either ways the arithmetic result will be the same Commented Feb 10, 2020 at 6:37
  • 2
    You can't do it without changing the data type. '01' ... '09' are strings, if you parseInt('01') then you'll have a number, console.log(01) will output as 1. You can't write a zero before single digits. Commented Feb 10, 2020 at 6:38
  • 2
    Numbers have no format, only a value. Doesn't matter wether you write 12, 0xC, 0.12e2 or 0b1100. All the same value. So work with the numbers! format them only when you render them. I don't know which Angular version you use, and I don't have experience with Angular.io, but in angularjs you could write a filter to do a job like this. Commented Feb 10, 2020 at 6:38
  • 1
    @Tanzeel you can do your calculations with or without a leading 0 the answer will never change. For representation you can do it using strings and all the answers show one way or another to do it Commented Feb 10, 2020 at 6:43
  • 1
    Sidenote @ellipsis: JS allows you to write numbers with leading zeroes. But they are a real issue, because sometimes they will get interpreted as octal and sometimes as decimal; and Tanzeel they still don't keep that leading zero because it's just another way to write a value. Commented Feb 10, 2020 at 6:44

3 Answers 3

14

You cannot add a 0 in front of a number, without also turning it into a string.

To do both:

(5).toString().padStart(2, '0');  // returns '05'
Sign up to request clarification or add additional context in comments.

3 Comments

But I've to perform some arithmetic with them :-(
@Tanzeel do arithmetic first, and only convert it to a string once you're ready to output it somewhere.
Second Param to padStart is a string e.g. You can do padStart(2, "0") in the above example.
1

Try something like this:

function convert(n) {
  n = String(n)
  if (n.length == 1)
    n = '0' + n
  return n
}

convert(0) //  -> "00"
convert(1) //  -> "01"
convert(9) //  -> "09"
convert(14) // -> "14"
convert(20) // -> "20"

Comments

0

I haven't used TypeScript much but since it is strongly typed, and you have initially declared monthStartingIndex as a number, I assume that's causing the error. Can you try the following?

Also, the snippet does not really make sense since you're converting to string and then using it for iteration. Maybe you want to do that inside the for loop?

monthSlice() {
    let monthStartingIndexValidate = "0";
    monthStartingIndexValidate = ("0" + monthStartingIndexValidate).slice(-2); //error
    let monthEndingIndex = 24;
    for(monthStartingIndex = 0; monthStartingIndex < monthEndingIndex; monthStartingIndex++) {
    ...
    } 
}

6 Comments

No Gaurav, I cant change the data type of my variables. there's a lot of arithmetic waiting to be implemented very soon.
@Tanzeel Then you need to do this in another variable using this value. I'm not sure what you want to do with this value, could you explain?
I'm doing this because I've to validate month. and the format is MM-DD-YYYY. That's why I want them to be 01, 02, 03, ...09 to comply with MM. Otherwise validation is failing. And I can't alter the format MM-DD-YYYY. My tech lead will not allow. :-(
Can you assign the padded value to a new variable with type string and then validate it?
Sorry Sir. I didn't understand this. Can you please explain a little bit more. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.