c# Compact framework Math.Round up problem [duplicate] - c#

This question already has answers here:
Why does Math.Round(2.5) return 2 instead of 3?
(15 answers)
Closed 2 years ago.
I am using VS2008 c# , windows mobile 6.5, so I have to use the .Net Compact Framework, the problem is:
When Math.Round(66.05,1) the result is 66 when the correc value should be 66.1
what can I do ? how should I use the round() function
Thanks

You should use the overloaded static Math.Round method which takes two arguments:
decimal x = Math.Round(2.5555, 2); // x == 2.56
You can read more here.
The issue with this is that the Mathf.Round method by default use "ToEven convention" There is an example using MidpointRounding.AwayFromZero that will give you the result you expect

Related

Correct way to precisely round decimals in C# [duplicate]

This question already has answers here:
How do I make it round off instead of always round down?
(2 answers)
How to round a decimal up?
(6 answers)
Closed 5 months ago.
This is my code:
decimal test1 = 190.5m;
decimal test2 = 191.5m;
var testResult1 = Math.Round(test1, 0); // =190 It should be 191!!!!
var testResult2 = Math.Round(test2, 0); // =192 Correct.
Is there a way I could get the correct result? Maybe another library instead of System.Math?
This is by design. Albeit not what we have grown up with.
Find how you manage to round the way you expect here:
https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding?redirectedfrom=MSDN&view=net-6.0
For a bit more reading and background, have a browse through some of the answers here:
Why does .NET use banker's rounding as default?

Calculating a String in C# [duplicate]

This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
how to convert a string to a mathematical expression programmatically
(6 answers)
Closed 2 years ago.
I want to calculate a string in C# using either NCalc or DynamicExpresso library, the problem is, when the calculation gets complex and the numbers are big, it returns the wrong result.
For example the code below returns -808182895 when it should return 3486784401
string value = "387420489*9";
value = new Interpreter().Eval(value).ToString();
Am i doing anything wrong?
Thanks for the help.
Try the following:
(long)387420489 * (long)9
Dynamic Expresso has a web shell here where you can test the expressions;
http://dynamic-expresso.azurewebsites.net/
While testing on this web shell, I realized that;
387420489L * 9 => Syntax error (at index 9). => does not accept type suffix
(long)387420489 * 9 => -808182895 => overflow
387420489 * (long)9 => 3486784401 => OK
2147483647 + 1 => -2147483648 => int.MaxValue + 1 = int.MinValue (overflow)
2147483648 + 1 => 2147483649 => When does not fit into Int32, interpreted as long
While most of these can be regarded as by design (considering how Dynamic Expresso evaluates the statement), there can still be further improvement.
Think of Javascript for example.
387420489*9 => 3486784401
The question is, is what we need
to execute the given arithmetic expression correctly, as we and the end-user expects,
to execute the given arithmetic expression the C# way?
The former, I think.

Round off decimal value in Nearest 10 or Zero [duplicate]

This question already has answers here:
C# - Math.Round
(5 answers)
Closed 4 years ago.
Following values i would like to convert to round off figure. likes:
60.72 --> 60.70
170.76 --> 170.80
Currently, I'm converted to round off value using below method:
getFee.ServiceRequestFee.ToString("N")
I'm not sure which Match.Round method suitable to my requirement.
Edit:
protected string Getroundoffdecimalvalue(string servicerequestsfee_val)
{
servicerequestsfee_val = Math.Round(Convert.ToDecimal(servicerequestsfee_val), 2).ToString();
return servicerequestsfee_val;
}
I used this function even after i return 60.72 only and my expectation should 60.70.
Math.Round(yourNumber, 1)
The second parameter is number of decimal places to round to. In your case you want 1 decimal place as an end result.
**
You need to overloadMath.round that takes the decimals parameter of
your choice and convenience.
**
Use Math.round and if needed convert the same to string.
Math.Round(var_name,2)

Math.Abs weird behaviour [duplicate]

This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);

Math.Round() seems to be not consistent [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.Net Round Bug
In C#: Math.Round(2.5) result is 2 (instead of 3)! Are you kidding me?
Code:
var d1 = Math.Round(187.5); // 188
var d2 = Math.Round(62.5); // 62
Why is it so?
By default, Math.Round uses a form of rounding called Banker's Rounding, which rounds to the nearest even integer when the input is halfway between two integers.
See Why does .NET use banker's rounding as default? for an understanding of this design decision.
If you don't like this behaviour, you can always use this overload of Math.Round, which lets you specify the MidPointRoundingMode (ToEven, AwayFromZero).
You can change this behaviour with an call to this overload of the method - http://msdn.microsoft.com/en-us/library/ms131274.aspx

Categories