Bizarre behavior on converting integer as string to number variable

Help with writing and running scripts

Bizarre behavior on converting integer as string to number variable

Postby Bracket » Tue Aug 02, 2011 11:17 am

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?
User avatar
Bracket
Basic User
Basic User
 
Posts: 35
Joined: Fri Oct 26, 2007 11:00 pm

Re: Bizarre behavior on converting integer as string to number variable

Postby jorrasdk » Tue Aug 02, 2011 3:01 pm

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);
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Bizarre behavior on converting integer as string to number variable

Postby Bracket » Tue Aug 02, 2011 3:27 pm

That was driving me nuts - thank you! :)
User avatar
Bracket
Basic User
Basic User
 
Posts: 35
Joined: Fri Oct 26, 2007 11:00 pm


Return to Scripts