This question already has answers here:
C# simple divide problem
(9 answers)
Closed 7 years ago.
This mabey is a dumb question but i try to count discount on a price like this:
newAllaVaror.Pris = System.Convert.ToDouble(
(1 - (clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)) * vara.Pris
).ToString();
It will look like this in reality (1-(20/100)*7.99), but my output is now 7.99 but it should be 6,392..becuse the orginal price is 7.99...
I have tried to move the parentheses but i only get 1 then..
In wich order does C# go thru the parentheses, becuse this should work right??
I'm pretty sure you are doing an integer division here:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)
So if you have 20/100, it will result in 0 instead of the expected 0.2 as the reminder is truncated.
You need to convert to double, one of the operands:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0)
Since #Scott pointed out your other variables are of type decimal, you need to convert one of the operands to that type instead:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0m)
Related
This question already has answers here:
Operator '<' cannot be applied to operands of type 'decimal' and 'double'
(3 answers)
Closed 2 years ago.
I'm new to programing and figured out that if you are dealing with money, decimal is better to use. I noticed that if I use a if statement like,
if (salePrice < 0.01)
If salePrice is a decimal, it will not work because .01 represents a double-precision floating-point number. If I change everything to double it does work. But, I need to keep everything as a decimal and you really can't mix decimal with double as far as I know. Setting it to if (salePrice == 0) will still show $0.00 by taking the itemCost - (itemCost * 99.99...%) is not what I want. Is there any way around this or am I stuck with if (salePrice < 1)?
Use the 'm' postfix to specify it's a decimal, not a float or double:
//This works
if (salePrice < 0.01m)
See the documentation for more details.
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");}
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/
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
This question already has answers here:
Math.Cos & Math.Sin in C#
(7 answers)
Closed 9 years ago.
I am trying to calculate sin(pi radians) using Math.Sin(). I am using the following code.
double degrees = 180;
double radians = Math.PI / 180 * degrees;
Console.WriteLine(Math.Sin(radians));
It is giving me a value of
1.22460635382238E-16
What is the reason for this? Surely it should be returning a value of 0 right?
Please enlighten me. Thanks.
1.22460635382238E-16 basically is 0. it actually represents the number
0.000000000000000122460635382238. This number is close to 0. It's basically just because of different rounding errors all coming together.
take a look at http://en.wikipedia.org/wiki/Floating_point for more information.