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.That is impossible in an absolute sense. You may be expecting that result, so you will have to check that the input value is correct: the issue is not with the Rounding method; it is with your input to the method.
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 | ||
---|---|---|---|---|
How to determine beginning of a new bar and Price bid/ask status within OnMarketData
by love2code2trade
Started by love2code2trade, Today, 09:32 PM
|
0 responses
7 views
0 likes
|
Last Post
![]() |
||
Started by Dr Kerry, Today, 09:24 PM
|
0 responses
6 views
0 likes
|
Last Post
![]()
by Dr Kerry
Today, 09:24 PM
|
||
![]()
Started by xiinteractive, 08-08-2023, 11:55 AM
|
26 responses
250 views
1 like
|
Last Post
![]()
by AndyM
Today, 07:23 PM
|
||
Started by tkaboris, Today, 04:56 PM
|
1 response
11 views
0 likes
|
Last Post
![]()
by tkaboris
Today, 07:12 PM
|
||
Started by dtaylor, 01-21-2022, 05:36 PM
|
6 responses
302 views
0 likes
|
Last Post
![]()
by dtaylor
Today, 07:00 PM
|
Comment