Math.Round(double value, 4)? This returned wrong value.
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!
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
NinjaTrader
Round double to four decimal places
Collapse
X
-
Originally posted by alexstox View PostI'm sorry. Maybe I was not correct. The true value is about 0.0135 (I counted manually). But method returned 0.0068. That what I mean.
Check the code that produces the result that you are trying to round: it must be in error, in terms of producing what you intend, as opposed to what is really written.
Comment
-
I'm running into a similar issue here...
My strategy is coded to do this equation:
Code:result= Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 )
0.99999441 is exactly what I want, and that's exactly what I get.
Now, if I want to take that number and others like it and make them a smidge more manageable, I want to take the inverse of it, or, subtract that result from 1:
1 - .99999441 = 0.00000559
And I would multiply that result by 10000 or something to get something even more manageable.
The problem is, in the very same strategy where this works just fine:
Code:result= Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 )
Code:modresult= Math.Round( 1 - result, 8 );
Code:modresult= Math.Round( (1 - Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 ) ), 8);
Why does the first example of rounding produce what I want, while the second one still forces it to scientific notation? Both variables are doubles. I want to get a value of 0.00000559. What am I missing here?
Thanks in advance,
Gary
Comment
-
Hello,
Thank you for the question.
This would be based on how C# handles double numbers.
In this case, the equation you have is basically correct but if you print the results the notation would be displayed instead.
If you take your example and plot the values you should see that the values are correct, only when you print the value would you see the notation.
To use the value in your equations, you can just use what you have now or what I was using in the test:
Code:double result = Math.Round((HMA(21)[1]/HMA(21)[0]), 8); double modresult= Math.Round( 1 - result, 8 );
To avoid the notation you could do this instead:
Code:Print(modresult.ToString("##.#########"));
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Waxavi, Today, 02:10 AM
|
0 responses
3 views
0 likes
|
Last Post
by Waxavi
Today, 02:10 AM
|
||
Started by TradeForge, Today, 02:09 AM
|
0 responses
8 views
0 likes
|
Last Post
by TradeForge
Today, 02:09 AM
|
||
Started by Waxavi, Today, 02:00 AM
|
0 responses
2 views
0 likes
|
Last Post
by Waxavi
Today, 02:00 AM
|
||
Started by elirion, Today, 01:36 AM
|
0 responses
4 views
0 likes
|
Last Post
by elirion
Today, 01:36 AM
|
||
Is it possible to have a drawing, such as a line, cycle through different colors?
by gentlebenthebear
Started by gentlebenthebear, Today, 01:30 AM
|
0 responses
4 views
0 likes
|
Last Post Today, 01:30 AM |
Comment