Announcement

Collapse
No announcement yet.

4 Decimal Places

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

  • 4 Decimal Places

    Hi,

    Every now an again I get Euro values with a very large amount of decimal places.

    How can I adapt the below script to make sure it only prints 1.xxxx when I use Math.round(close())


    function rnd(value){
    value *=100;
    return Math.round((value, 2)/100)}

    Thanks Roger

  • #2
    Hi rogerha,

    Try adding this to your return statement:

    .toFixed(4)*1

    where .toFixed(4) modifies the value to a string with 4 decimal places to the right, then multiplying by 1 converts to a number again.

    Having said that, you may want to just use only .toFixed(4)*1 as it rounds as well.

    Comment


    • #3
      Hi Steve, Thanks,

      I am not sure if I understood you correctly, but this is the syntax pertaining to this that I have now:


      function rnd(value){
      value *=100;
      //return Math.round((value, 2)/100)
      return Math.toFixed(4)*1}

      drawTextRelative(0, 20, (vTrend+(Math.toFixed(close()))), Color.green, null, Text.RELATIVETOBOTTOM|Text.ONTOP|Text.FRAME|Text.B OLD,"Arial",10,"enter"+nCntr);

      Without any success I am afraid, I do apologise if I am being a bit of a doofus But can you correct me please ?

      Comment


      • #4
        Hi Roger,

        You can do this several ways.

        First, you can affix .toFixed(4) to any variable that you are displaying or otherwise output.

        e.g.

        var a = 1.23456

        a.toFixed(4) = 1.2346

        Be cautious though, as this is now a string, you have to multiply by 1 to convert back to a number. Also remember that strings will return to the cursor window (if in the return statement), but will not be plotted to the chart.

        As far as your function, you can try this

        function rnd(value){
        return value.toFixed(4)*1;
        }

        or

        function rnd(value){
        value *=10000;
        return (Math.round(value)/10000);
        }

        I was thinking in the second example to add .toFixed(4)*1 after the parenthesis

        like this:

        (Math.round(value)/10000).toFixed(4)*1;

        However, that is a bit redundant and I would not go that route. I hope this is more helpful.
        Last edited by ; 02-07-2005, 01:02 PM.

        Comment


        • #5
          Steve, this worked a treat. Many thanks for the full explanation, your time and expertise.

          Comment


          • #6
            Hi Roger,

            You are most welcome.

            Comment

            Working...
            X