Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Math issue dividing an integer

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

    Math issue dividing an integer

    OK, here's my issue. I have an integer value that I want to divide by 100 and assign the results to a double. e.g. below:
    int iIntValue;
    double dOrderPrice;
    double dResult;

    iIntValue = 20;
    dOrderPrice = Position.AveragePrice;
    dResult = dOrderPrice - (iIntValue / 100);

    When I do a Print(dOrderPrice + " : " +dResult + " : " +iIntValue);
    I get:
    87.69 : 0 : 20
    when I expect to get:
    87.69 : 0.2 : 20

    I should be able to divide an integer and get a double. Is there a reason that this math would not work?

    #2
    Hi, thanks for posting. You should cast the 100 to a double ((double)100) or add a .0 to the end of the value to make it a double and iIntValue should also be a double. It will truncate the fractional value if you use an integer.

    Kind regards,
    -ChrisL

    Comment


      #3
      I did a work around by assigning the int value to a local double variable and then dividing by 100.
      I have not seen this behavior before.

      Comment


        #4
        Thank you for the feedback. I verified it does the same in Visual Studio for C#. Apparently I don't code enough C# to notice.

        Closed.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        43 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        21 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        31 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        50 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Working...
        X