Math.Round(double value, 4)? This returned wrong value.
Announcement
Collapse
No announcement yet.
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:
That's this: 48.016203463203 / 48.015935064935 = 0.99999441Code: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:
this does not:Code:result= Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 )
and neither does this:Code:modresult= Math.Round( 1 - result, 8 );
because both return a value of this: 5.59E-06.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:
Plotting modresult works but Print does not, this is because of how ToString works with doubles.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:
This just specifies that we want to print the additional decimal places. Anywhere you print the value you would need to use the ToString("##.#########") otherwise anywhere you are just checking the value you can leave the variable as is.Code:Print(modresult.ToString("##.#########"));
I look forward to being of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
579 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
334 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

I don't understand, why everything is OK now. I will answer later. Will try to reproduce error =)
Comment