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

Print - result of division

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

    Print - result of division

    All,

    Here's an interesting one, a simple print statement with an expected result of "division 75.00%". Oddly the return is "division 0.00%"

    Code:
    Print ("division " + (3 / 4).ToString("0.00%"));
    This works as expected for 3 * 4, but the division results in a return of 0

    How can I get around this?

    Regards
    Shannon

    #2
    Shannon, try casting them to doubles to prevent false implicit conversions from the compiler -

    Print ("division " + ((double)3 / (double)4).ToString("0.00%"));

    or

    Print ("division " + ((double)3 / 4).ToString("0.00%"));

    shows correctly 75.00%
    Last edited by NinjaTrader_Bertrand; 10-20-2010, 05:44 AM.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand,

      Thanks for the quick reply. Two questions:
      1. What's the difference between and appropriate use of (double)xx vs. Convert.ToDouble(xx)?
      2. As a matter of programming etiquette, where the result of a division could be a double, should integers be converted to doubles beforehand?

      Thanks again
      Shannon

      Comment


        #4
        Shannon, either or should be fine, you can convert or cast the value (I believe converting would be a tad bit quicker than casting it).

        Correct on 2, if you like to get a double outcome input integers like 1000.0 to achieve it.
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by alifarahani, Today, 09:40 AM
        6 responses
        33 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        14 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by Waxavi, Today, 02:00 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by gentlebenthebear, Today, 01:30 AM
        3 responses
        17 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X