The null-coalescing operator has a lower precedence than the divide operator. Therefore, your code is interpreted as
container.promotion_rate ?? (0 / 100m)
which gives you a different result.
Domanda
Hello i have a doubt about this mathematical function in C# and i want to know why this happens. This two lines give different results, why?
container.price * (1 + (container.tax_rate / 100m)) * (1 - (container.promotion_rate ?? 0 / 100m))
Returns -> -839.9944
container.price * (1 + (container.tax_rate / 100m)) * (1 - ((container.promotion_rate ?? 0) / 100m))
Returns -> 50.99966
And the only difference in the function is the parenthesis on the second function: (container.promotion_rate ?? 0)
Soluzione
The null-coalescing operator has a lower precedence than the divide operator. Therefore, your code is interpreted as
container.promotion_rate ?? (0 / 100m)
which gives you a different result.
Altri suggerimenti
/
has a higher precedence than ??
.
Your first expression is parsed as container.promotion_rate ?? (0 / 100m)