• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Rounding floating point number to a specified accuracy.

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

    Rounding floating point number to a specified accuracy.

    I'm looking to round float number to 0 number of digits after the decimal point.

    Something similar to

    NormalizeDouble
    Rounding floating point number to a specified accuracy.
    Code:
    double  NormalizeDouble( 
       double  value,      // normalized number 
       int     digits      // number of digits after decimal point 
       );
    Code:
    /* 
       Result: 
       pi= 3.1415926535897931 
       NormalizeDouble(pi,3)= 3.1419999999999999 
       NormalizeDouble(pi,8)= 3.1415926499999998 
       NormalizeDouble(pi,0)= 3.0000000000000000 
    */

    #2
    Hello johnnybegoode,

    I'm looking to round float number to 0 number of digits after the decimal point.
    To confirm: you want to round the number to the nearest whole integer or you want to create a function similar to what you have described that is variable and rounds to a specified accuracy? In either case you can likely use Math.Round.

    This would be a good question to search for online just as a general C# search to gather some opinions, there is likely an existing StackOverflow or other sites where this has been asked/solved already in C# as well. There is nothing specific in NinjaScript which uses a specified accuracy to round however there are methods to round to tick sizes.

    Here is one page which describes some ways to round to the nearest whole number with some examples:
    https://stackoverflow.com/questions/...ber-in-c-sharp

    The linked MSDN document for the Math.Round used: https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=netframework-4.5#System_Math_Round_System_Decimal_System_Int32_ System_MidpointRounding_

    Code:
    Print(Math.Round(1.1, 0)); // Output: 1
    Print(Math.Round(1.5, 0, MidpointRounding.AwayFromZero)); // Output: 2
    Print(Math.Round(1.9, 0)); // Output: 2
    Print(Math.Round(2.5, 0)); // Output: 2
    Print(Math.Round(2.5, 0, MidpointRounding.AwayFromZero)); // Output: 3
    Print(Math.Round(3.49, 0, MidpointRounding.AwayFromZero)); // Output: 3
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Turned out 2 things I need

      1) Rounding a double value to the 2 significant figures
      i.e. 64,492
      (a) 1 significant figure which is 60,000
      (b)2 significant figures which is 64,000
      (c) 3 significant figures which is 64,500
      (d) 4 significant figures which is 64,490
      (e) 5 significant figures which is 64,492

      then

      2) How do I convert that double value to int so that it could be accepted by
      ExitLong(int quantity)
      Last edited by johnnybegoode; 01-03-2019, 03:31 AM.

      Comment


        #4
        Hello johnnybegoode,

        These would also be good questions to search for general C# answers online, these concepts would not be NinjaScript specific and there are no NinjaScript functions for these purposes.

        To round to the significant digit, you would likely need to create a function to do this. I did a quick search online but was unable to locate something quickly. This would likely require you to use math in a function to check if the input value and the X significant value is greater/lesser than the midpoint and then add or subtract to the next interval. If you are using a TickSize which is this interval, you could use the RoundToTick functions:

        https://ninjatrader.com/support/help...ghtsub=roundto
        https://ninjatrader.com/support/help...toticksize.htm
        https://ninjatrader.com/support/help...ghtsub=roundto

        I would suggest doing a search for these terms on google to find some other examples of creating functions and math in C# to accomplish this.

        To convert a number, you can use the types Convert function, or use a cast. You can read more about these topics at MSDN:

        https://docs.microsoft.com/en-us/dot...tframework-4.5

        https://docs.microsoft.com/en-us/dot...pe-conversions



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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by firstlanetech, Today, 01:05 AM
        1 response
        3 views
        0 likes
        Last Post firstlanetech  
        Started by HiddenPhilosopher, Yesterday, 11:58 PM
        0 responses
        3 views
        0 likes
        Last Post HiddenPhilosopher  
        Started by glennts, Yesterday, 08:24 PM
        2 responses
        15 views
        0 likes
        Last Post bltdavid  
        Started by ibabinov, 11-12-2019, 09:50 AM
        3 responses
        66 views
        0 likes
        Last Post NinjaTrader_ShawnB  
        Started by PrimoItch, 05-27-2020, 04:35 PM
        2 responses
        18 views
        0 likes
        Last Post bkonia
        by bkonia
         
        Working...
        X