Announcement

Collapse
No announcement yet.

Converting an interval in a number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting an interval in a number

    Hi,
    how can I convert an interval less than a minute found with getInterval () (say "15s") in a number ("15") ?
    Thanks in advance.
    Massimo

  • #2
    Reading about Strings I found this solution to my problem.
    Maybe it can be useful for someone else.
    Regards
    Massimo

    function preMain() {
    setPriceStudy(false);
    debugClear();
    }

    function main() {

    if (isLastBarOnChart()) {
    var a=getInterval();
    debugPrintln("Interval = "+a);
    var b=a.length; //
    debugPrintln("Length = "+b);
    if (a.search("S") != -1) var c=a.slice(0,-1)*1;
    else var c=a*1;
    debugPrintln("Number = "+c+");
    }
    }

    Comment


    • #3
      It's even easier than that:

      var num = parseInt("15S");

      returns 15

      Note:

      In Javascript, always place your "var declarations" at the top of your functions (just after the "{"). There is no such thing in Javascript as either block local scope or scope from declaration to the end of a block or function. They're just going to get automatically hoisted to the top of the function definition for you by the interpreter and that can produce some hard-to-find bugs if you accidentally reference the variable before you first initialized it.

      All variables in Javascript are either global or they're local to the *entire* respective function definition.

      Comment


      • #4
        Steve,
        thanks for your suggestions.
        Massimo

        Comment

        Working...
        X