C # Math.Round (). Only one sentence works for me.


Why does the first rounding statement work for me and the second does not?

//Esta Funciona  
decimal discountAmount = Math.Round(subTotal * discountPercent, 2);  

//Esta no funciona  
decimal discountAmount = subTotal * discountPercent;  
Math.Round(discountAmount, 2);  
asked by Hayweistar 29.03.2017 в 04:12

1 answer


Both work, what happens is that one is assigned, the other is not.

When doing:

Math.Round(...); // Donde los puntos son tu expresion aritmetica o valor.

You are not assigning the return value of Math.Round(...) , you only make the operation and nothing else happens afterwards.

But when doing:

decimal t = Math.Round(...); // Lo mismo con los puntos...
Console.WriteLine(t); // Debe de darte el round del valor.

You are assigning the result of the function Math.Round in variable t (In my case) .

Functions have a return value, that value is assignable to a variable or used in an expression, example:

int dos() {
    return 2; // Devuelve 2 a quien me llame.

If I do:

decimal MiDecimal = Math.Round((dos() * 8.25) + 0.05, 1); 

That is summarized to:

decimal MiDecimal = Math.Round((2 * 8.25) + 0.05, 1);

And carry out the operation, where 16.5 is the value of MiDecimal .

answered by 29.03.2017 / 04:22