Announcement

Collapse
No announcement yet.

Number of decimal places of a number that converts to string

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

  • Number of decimal places of a number that converts to string

    Hello,

    I have converting a number in EFS to string like the closing price. How to I make sure it only shows 2 decimal places?

    -Mike

  • #2
    Hi,

    I often use the following function placed outside of main() and called anywhere using:

    rnd(number, decimal places);


    /// example start ///
    var x=54.940003324868;
    debugPrintln(rnd(x,2)+"\t"+Math.round(x));
    /// example end ///


    function rnd(value, N) {//N = round to N # of digits
    if(N==null || typeof N=="undefined") N=Dec_Len(tick);
    var n;
    var mult=1;
    for(n=0;n<N;n++) mult*=10;
    value*=mult;
    return Math.round( value,N)/mult;
    }

    Wayne

    Comment


    • #3
      You can also use toFixed(# of digits). It rounds to the "# of digits" and returns a string.

      Syntax: close(0).toFixed(2);

      You can convert it back to a number by multiplying the value times 1:

      var valueTxt= close(0).toFixed(2);//assigns close(0) rounded to 2 digits as text to valueTxt
      var valueNum=valueTxt*1;//assigns valueTxt to valueNum as a number


      Wayne

      Comment

      Working...
      X