Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Round double to four decimal places

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

    #16
    Wow, C# doesn't make it easy, does it? I liked your first solution, too, and I started experimenting along those lines before you updated your reply:


    ---
    Thanks for this, and it has moved me one step closer, but I'm still failing to get the final answer I want, which is that finalResult subtracted from 1.

    I experimented with this:
    Code:
    double calcResult = (1- finalResult);
    But I ended up right back where I was before, with a scientific notation result.

    I suppose I could take this calcResult and apply your initial suggestion a second time, or just leave it alone since you have since said the system treats the numbers properly and it's only Print that messes it all up... but isn't there some simple and obvious (although clearly not to me) way to just add that extra " 1 - " to your initial recommended code so that the value in finalResult would be the equivalent of
    Code:
    decimal num = Decimal.Parse(result.ToString(), System.Globalization.NumberStyles.Float);
    double finalResult = (double)Math.Round(num, 8);
    [I]calculatedResult[/I] = ([B]1 -[/B] (double finalResult = (double)Math.Round(num, 8)));
    ?
    It may seem silly, but I really like using the Output Window as I go, to confirm that I'm on the right path...

    Comment


      #17
      Hello,

      Yes the first solution is good but after writing it I realized I had skipped the subtraction which in turn makes the double print as notation again. This solution would be more tuned to a decimal end result from a exponential double so this would not exactly fit for the solution.

      In the revised answer I realized that you are only seeing the exponential number because of how the Print method works, otherwise the actual number did not change from a double into notation. This makes the first answer over complicated for what is happening.

      so in turn this:

      Code:
      double calcResult = (1- finalResult);
      should return a number like this internally:
      Code:
       -0.00004114
      unless you do a Print or
      Code:
      Print(calcResult);
      in that case the output would display the notation, the underlying number did not change its just the Print method cant handle a number that long.

      As long as you just do ToString("##.########") for anything going to the output window that should help with seeing the data printed.

      When you Print a number it automatically makes that number into a string for the output window, without telling the compiler that we need to see more decimal places it just converts the string result into notation if it is too long.

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #18
        Jesse, thanks.

        Comment


          #19
          The builtin Round2TickSize() method might be of value here.

          See this page:


          To make this easier to type, I always encapsulate this in a separate function, like this,

          Code:
          private double Round2TickSize(double DoubleValue)
          {
              return Instrument.MasterInstrument.Round2TickSize(DoubleValue);
          }
          And then call it when I deal with price values, for example,

          Code:
          SlowMA = Round2TickSize(EMA(SlowMAPeriod)[0]);
          FastMA = Round2TickSize(EMA(FastMAPeriod)[0]);
          Print("SlowMA=" + SlowMA + " FastMA=" + FastMA);
          Regards,

          David

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by benmarkal, Yesterday, 12:52 PM
          3 responses
          22 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by helpwanted, Today, 03:06 AM
          1 response
          18 views
          0 likes
          Last Post sarafuenonly123  
          Started by Brevo, Today, 01:45 AM
          0 responses
          11 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          6 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          244 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Working...
          X