Announcement

Collapse
No announcement yet.

cut off to 2 or 3 decimals

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • cut off to 2 or 3 decimals

    I'd like to know how to take a price (let's say a MA calculated) and round the number to 2 or 3 decimals.

    Since, the MA will have more decimals, I need a function to drop the extra decimals but not make it an integer of course...

    45.3258922 => 45.33

    thanks

  • #2
    Dion posted a nice solution to this a while ago:

    function rnd(value) { // Round the price to two digits
    value *= 100;
    return Math.round(value, 2)/100;
    }

    Just put this at the start of your efs file (not in Main() or PreMain()).

    You could then do:

    nResult = Math.round(nMyMA);

    And nResult will be nMyMA rounded to two places.

    G
    Garth

    Comment


    • #3
      Slight correction, the line to do the rounding should read:

      PHP Code:
      nResult rnd(nMyMA); 

      Comment


      • #4
        Ooops. Yep. Thanks for catching that.
        Garth

        Comment


        • #5
          thanks

          thanks guys,
          I did caught the problem..

          works like a charm

          Comment


          • #6
            This one rounds to N digits:

            function rnd(value, N)
            { // Round the price to N digits
            var n;
            var mult = 1;
            for (n=0; n<N; n++) mult*=10;
            value*=mult;
            return Math.round(value, N)/mult;
            }

            Comment


            • #7
              round up to the nearest 0.25

              rUP = ma-ma%0.25+0.25

              rDn = ma-ma%0.25

              Comment


              • #8
                truncating decimals..

                Here is a code segment that I use to truncate decimal values..

                Let's say your indicators returns a value like...

                35.975847564785667843

                and you only want it to report back

                35.9758

                This is the code. It creates a "TEXT LABEL" that can be placed on the chart or in the cursor window. It DOES NOT ROUND. It simply truncates the value.

                vSUM is your assumed indicator value.
                PHP Code:
                       var i;
                       var 
                DecLength 4;  // number of decimals to include

                       
                var Multiplier 10;
                         for (
                i=1;i<DecLength ;i++) {
                          
                Multiplier Multiplier *10;
                         }

                       var 
                TextvSUM ""+(Math.floor(vSUM))+".";
                       var 
                Fraction ""+(Math.floor((vSUM-Math.floor(vSUM))*Multiplier ));

                     
                //  fill in additional leading ZEROs (if necessary).
                       
                if (Fraction.length DecLength ) {
                         for (
                i=1;i<=(DecLength -Fraction.length);i++) {
                          
                TextvSUM += "0";
                         }
                       } 
                       
                TextvSUM += Fraction
                TextvSUM now returns your TEXT value of your truncated indicator (or price).
                Brad Matheny
                eSignal Solution Provider since 2000

                Comment

                Working...
                X