This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 2 years ago.
Decimal Value is 12568.521
When converting into float
(float)Value
Value is coming as 12568.5215. I need exact value as 12568.521. Please anyone guide me.
Try using Round in Math
decimal value = 12568.5215;
return Math.Round(value,3);
You may convert the decimal variable into a string:
decimal d = new decimal(12568.521);
float f = Single.Parse(d.ToString());
Related
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I feel it's a bug, is it?
decimal s = 30 / 9;
double w = 20 / 9;
Console.WriteLine(s);
Console.WriteLine(w);
and the result is
Is this normal, yes. Is this a bug, no.
You're doing integer math on 30 / 9 which is 3 and then assigning to a decimal which then does the conversion. Same with the second line.
Try this instead:
decimal s = 30m / 9m;
double w = 20.0 / 9.0;
Console.WriteLine(s);
Console.WriteLine(w);
That gives:
3.3333333333333333333333333333
2.2222222222222223
You need to declare your constants in your expression with the correct type. Suffix an m for decimal. And either provide a decimal place or suffix with a d for double.
This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Closed 3 years ago.
I need to calculate x^y where both x and y are Doubles.
I tried using Math.Pow:
Double result = Math.Pow(24.69, 2/3);
The value of result is 1 where it should be 8.4790 ...
Any idea why?
For the exponent you are passing in 2 ints which is do integer division. So it is doing:
Math.Pow(24.69, 0)
To fix this use doubles like this:
Double result = Math.Pow(24.69, 2.0/3.0);
This question already has answers here:
Implicitly converting int to double
(8 answers)
Convert Double Value ToString() [duplicate]
(1 answer)
C# Calculations With Decimal Places
(5 answers)
Closed 5 years ago.
I'm trying to make a program to work out sin cos and tan for my uncle. i'm pretty new to c#. i made you have to type in three text boxes and it would then take the information and divide it and output the answer into a label but it would keep on rounding it off please help. here is my code:
int triA = Int32.Parse(enterA.Text);
int triB = Int32.Parse(enterB.Text);
int triHYP = Int32.Parse(enterHyp.Text);
Double sinAns = triA / triHYP;
Double cosAns = triB / triHYP;
Double tanAns = triA / triB;
sinAnswear.Text = sinAns.ToString();
cosAnswear.Text = cosAns.ToString();
tanAnswear.Text = tanAns.ToString();
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 9 years ago.
Why is my float automatically rounding and how do i get it to stop
float pageCount = 10/6;
should be 1.666
but it is giving 1.0
Your calculation is being done in integer type since both the operands are of int type
cast or mark atleast one of the operand as float.
float pageCount = 10/6f; //6f specifying 6 as float
or
float pageCount = ((float) 10)/6;
In your current form, both operands are of integer type and their division results in integer value that is why you get 1 not 1.666
Simple to the following 10.0/6
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 9 years ago.
If I divide 150 by 100, I should get 1.5. But I am getting 1.0 when I divided like I did below:
double result = 150 / 100;
Can anyone tell me how to get 1.5?
try:
double result = (double)150/100;
When you are performing the division as before:
double result = 150/100;
The devision is first done as an Int and then it gets cast as a double hence you get 1.0, you need to have a double in the equation for it to divide as a double.
Cast one of the ints to a floating point type. You should look into the difference between decimal and double and decide which you want, but to use double:
double result = (double)150 / 100;
Make the number float
var result = 150/100f
or you can make any of number to float by adding .0:
double result=150.0/100
or
double result=150/100.0
double result = (150.0/100.0)
One or both numbers should be a float/double on the right hand side of =
If you're just using literal values like 150 and 100, C# is going to treat them as integers, and integer math always "rounds down". You can add a flag like "f" for float or "m" for decimal to not get integer math. So for example result = 150m/100m will give you a different answer than result = 150/100.