Math.Abs weird behaviour [duplicate] - c#

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);

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?

Is there a way to display 2 decimal places instead of automatically rounding? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 3 years ago.
I'm making a simple calculator in c# and and everything works well until I try dividing numbers that should give decimal places. How to display 2 decimal places in these cases?
I've tried putting #.## after .ToString.
{label1.Text = (divide / Convert.ToInt64(label1.Text)).ToString("#.##");}
I expect the output of 5/4 to be 1.25, but it is 1.
convert the number to float instead of int,
{label1.Text = ((float)divide / float.Parse(label1.Text)).ToString("n2");}

how to divide number using double or decimal in c# [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
when divide in c# Answer is different and when i divide from calculator answer is different kindly solve it.
c#
double div=(double)100000/30/9;
Answer 370.37037037037038
370.37037037037038*9*30 //100000.0000000000026
in calculator
Answer 370.3703703703704
370.3703703703704*9*30 //100000
i need exact answer like a calculator.
The difference is simply one of rounding. That is not a rational number, it has a repeating sequence, and cannot be perfectly expressed in binary code. It cannot even be expressed with a finite number of digits in decimal. The only difference there is that your calculator displays fewer digits than the C# double.
The final '04' is simply your calculator rounding '038' upward.

why does double return this value? C# [duplicate]

This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
decimal vs double! - Which one should I use and when? [duplicate]
(7 answers)
Closed 6 years ago.
double sth = 250 - 249.99;
Console.WriteLine(sth);
Why does this return sth like 0.009994507, instead of 0.01?
Floating point numbers (in this case doubles) cannot represent decimal values exactly. For more info, see this page here
If you need a more accurate representation, use decimal instead.
because when you print the double you print the all double value not just the first x after point digits.
you can use String.Format to print only the first 2 numbers.
double sth = 250.00d - 249.99d;
string sthString = String.Format("{0:0.00}", sth);
Console.WriteLine(sthString);
There are a lot of decimals that have infinite binary representation. What you're experiencing is exactly this case.
For more on this topic see: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/

taking just two digits of a Number of type double without rounding it [duplicate]

This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 8 years ago.
I have this Number
double Nb=4.9584763251
how can I make get only two digits after the comma without rounding the number :
Nb=`4.95` and not `4.96`
using the Math.Round(Nb,2) will return 4.96
is there any way to fix this ?
You may use Math.Truncate(Nb * 100) / 100m
double x = ((int)(Nb*100))/100.0

Categories