Announcement

Collapse
No announcement yet.

.toFixed not working

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

  • .toFixed not working

    I have a program that gets the current value of the 34 period exponential EMA. I want the value reported in the return line at the end of the program.
    I have tried two different return statements :
    return("34="+v34EMA);
    return("34="+v34EMA.toFixed(3));
    The first one works perfectly. But when I try to truncate the value to 3 decimal places using the.toFixed thingie, then I get an error message saying that the variable "v34EMA" "has no properties." I have no clue what that means. It's a simple real number, and I only want it to report to 3 decimal places.

    How do I get this value to report to only 3 decimal places?
    Attached Files

  • #2
    f1sh7,

    When you return a text value to the chart, it only goes to the cursor window. What happened is that the .toFixed() function converts numbers to strings. An easy way to convert strings back to a number in Javascript is to multiply by 1, for example:

    return("34="+v34EMA.toFixed(3)*1);


    Another problem however is the format of the return line. It should be

    return(v34EMA.toFixed(3)*1);



    hope this helps.

    Comment


    • #3
      John
      In addition to what Steve said if you want to use .toFixed() you need to run a null check on the value first.
      The enclosed revision of your script should now work
      Alex

      PHP Code:
      var v34EMA null;

      function 
      preMain() {

          
      setPriceStudy(true);



      function 
      main() {

          
      v34EMA ema(34,0);
          if(
      v34EMA==null) return;
          
      //return("34="+v34EMA);
          
      return("34="+v34EMA.toFixed(3));

      Comment


      • #4
        Alex,

        Thanks.. that was it... it was simply that I needed to do the null check first.

        Now I got it working exactly as wanted.

        THNX!

        Comment


        • #5
          John
          You are most welcome
          Alex

          Comment

          Working...
          X