Announcement

Collapse
No announcement yet.

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.

      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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          574 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          333 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X