Bizarre behavior on converting integer as string to number variable

Bizarre behavior on converting integer as string to number variable

30
Basic UserBasic User
30

    Aug 02, 2011#1

    I'm working on a script that converts PM times to 24 hour format.

    The core of that script is this:

    Code: Select all

    var WorkingFile = UltraEdit.activeDocument;
    
    WorkingFile.copy();
    
    Hour = UltraEdit.clipboardContent;
    
    Hour = parseInt(Hour)
    
    if (Hour != 12)
    {
    	Hour += 12;
    }
    
    UltraEdit.clipboardContent = Hour;
    
    WorkingFile.paste();
    As this is, you can select any number (I'm working with two digit numbers), run the script, and it should make the change. However, here's what the numbers become:

    01 - 13
    02 - 14
    03 - 15
    04 - 16
    05 - 17
    06 - 18
    07 - 19
    08 - 12
    09 - 12

    10 - 22
    11 - 23

    What the heck is causing this?

    262
    MasterMaster
    262

      Aug 02, 2011#2

      Hi!

      parseInt takes two arguments: The mandatory string to be converted to integer and an optional argument: Base (radix) of the number.

      Without the second argument general Javascript syntax for numbers apply. Thus numbers starting with 0 are octal (numbers 00 to 07). "08" and "09" is an invalid octal and parseInt returns 0.

      What you want is to use base 10 (decimal) as the second argument: Hour = parseInt(Hour,10);

      30
      Basic UserBasic User
      30

        Aug 02, 2011#3

        That was driving me nuts - thank you! :)

        1
        NewbieNewbie
        1

          Jun 02, 2015#4

          More about.......String to Integer conversion

          Biden