i have following case
beleg.PreisDB = (double?)orders.Where(x => x.orderId == beleg.auftrnr).Sum(x => x.itemPrice + x.shippingPrice + x.giftWrapPrice) ?? 0;
beleg.PreisCouponDB = (double?)orders.Where(x => x.orderId == beleg.auftrnr).Sum(x => x.itemPromotionDiscount + x.shipPromotionDiscount) ?? 0;
var gesamtPreis = Math.Round(beleg.PreisDB??0 + beleg.PreisCouponDB??0, 2);
I have added a quickwatch in debug to some fields in my case:
beleg.PreisDB == 8.39
beleg.PreisDB??0 == 8.39
beleg.PreisCouponDB == -0.49
beleg.PreisCouponDB??0 == -0.49
And now the strange behaviour also from quickwatch and of course the result
beleg.PreisDB??0 + beleg.PreisCouponDB??0 == 8.39
Math.Round(beleg.PreisDB??0 + beleg.PreisCouponDB??0, 2) == 8.39
gesamtPreis == 8.39
So the addition of 8.39 + -0.49 doesn't give me 7.9 but 8.39 This code was running for 600k cases on at least two i had this behaviour the others behaved well. I'm to blind to see my error at the moment. The question is why is .net behaving like this? I'm Using Visual Studio 2015 with .net 4.5.2.
printf("Enter two integers: "); scanf("%d %d", &number1, &number2); Then, these two numbers are added using the + operator, and the result is stored in the sum variable. Finally, the printf() function is used to display the sum of numbers. printf("%d + %d = %d", number1, number2, sum);
In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.
In this program, the user is asked to enter the number of rows r and columns c . Then, the user is asked to enter the elements of the two matrices (of order rxc ). We then added corresponding elements of two matrices and saved it in another matrix (two-dimensional array). Finally, the result is printed on the screen.
Addition of two numbers in C. Addition of two numbers in C: This C language program performs the basic arithmetic operation of addition of two numbers and then prints their sum on the screen. For example, if a user will input two numbers as; '5', '6' then '11' (5 + 6) will be printed. Addition program in C. int a, b, c;
In C Programming, Addition Operator is used to find the sum of two numbers. The operator takes two operands and returns the sum of these two operands. In this tutorial, we shall learn about Arithmetic Addition Operator and its usage with examples.
The input () function, which takes no arguments from the main method, takes input from the user and returned this value to the main function. The addition () function takes two arguments because it will add two numbers. To add two numbers, first of all, numbers should be passed to the addition () function.
Program to Add Two Integers. In this program, user is asked to enter two integers. Two integers entered by the user is stored in variables firstNumber and secondNumber respectively. This is done using scanf() function. Then, variables firstNumber and secondNumber are added using + operator and the result is stored in sumOfTwoNumbers.
The problem is precedence - +
has higher precedence than ??
, so it "binds tighter".
Here's a complete example to demonstrate:
using System;
class Test
{
static void Main()
{
double? x = 8.39;
double? y = -0.49;
// Your expression
Console.WriteLine(x ?? 0 + y ?? 0);
// The equivalent you're expecting
Console.WriteLine((x ?? 0) + (y ?? 0));
// The actual bracketing
Console.WriteLine(x ?? ((0 + y) ?? 0));
}
}
Another alternative would be to use Nullable<T>.GetValueOrDefault()
instead of ?? 0
:
Console.WriteLine(x.GetValueOrDefault() + y.GetValueOrDefault());
But I think I'd probably just use the version with brackets - so in your case:
var gesamtPreis = Math.Round((beleg.PreisDB ?? 0) + (beleg.PreisCouponDB ?? 0), 2);
I would definitely put the spaces in round ??
just like most other operators, as otherwise it's giving you the impression of binding very tightly (like the .
operator does).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With