decimal/double/float won't store decimal places [duplicate] - c#

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
I have tried this with decimal, double and float variables. I am dividing an integer by another integer and storing the result in a variable. None of the three data types will store decimal points, for example:
double d;
uint num1 = 20
uint num2 = 3
d = num1 / num2;
//d = 6.0
It is as if it is rounding to the nearest integer, help please?

You should cast either num1 or num2 as a decimal/double/float first before doing the division and storing the result..

When you do math with integers, the result is an integer. That's just how the operators are defined. To do double math, make num1, num2, or both doubles, or cast one of them to a double before calculating.

Related

What values integer gets? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
What values integer gets? When i wrote this code the output was 0 and not 0.333333
int a=18;
Int b=6;
int c=b/a;
Console.WriteLine(c);
thank you (:
int can not show Decimal. you must use float or double
The answer is in your question, integer can only hold integer value (...,-2,-1,0,1,2,...) so 6/18 would be 0. You could expect otherwise if 2 of these conditions are met:
1.c (the variable result is assigned) can hold floating type (float, double, decimal)
either an implicit or an explicit cast is done.
To describe (2) a bit more:
if either of a or b is of floating point type, you implicitly casting the result to a floating point value.
double a = 6;
int b = 18;
double c =a / b;
or you explicitly say that the int values (at leat one of them) should be considered as floating point:
int a = 6;
int b= 18;
double c = (double)a/(double)b;
double c = (double) a/b;
All you need to know about types
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/types

Differences in double and decimal calculations in C# [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I am getting different results for the double and decimal calculations...
double value1 = 280.585 - 280.50;
decimal value2 = Convert.ToDecimal(280.585) - Convert.ToDecimal(280.50);
Console.WriteLine(value1);
Console.WriteLine(value2);
Output:
Double:0.0849999999999795
Decimal:0.085
But how come int and long give the same results?
int value1 = 2+2;
long value2 = 2+2;
Console.WriteLine(value1);
Console.WriteLine(value2);
Output:
4
4
280.585 and 280.5 are both exactly representable as short decimal fractions, as is their difference.
Assuming double is represented as IEEE 754 64-bit binary floating point, the closest double to 280.585 is 280.58499999999997953636921010911464691162109375. 280.5 is exactly representable as a double. Their difference is 0.08499999999997953636921010911464691162109375.
In the case of the 2+2=4 calculation, all the numbers are exactly representable in either int or long so both should get the exact answer.

What is the difference between Convert.ToInt32 and (int) Parsing [duplicate]

This question already has answers here:
Converting a double to an int in C#
(5 answers)
Closed 6 years ago.
I can't understand the difference between Convert.ToInt32 and Parsing (int) command when i convert a double number to a int number.My example code is here and i have two different answer when i show it.
class Program
{
static void Main(string[] args)
{
double i = 3.897456465;
int y;
y = Convert.ToInt32(i);
Console.WriteLine(y);
y = (int)i;
Console.WriteLine(y);
Console.ReadKey();
}
}
(int) i; is casting not parsing.
Convert.ToInt32(double) would round the number to nearest 32 bit integer. Whereas casting (int) i; would only take the integer part of the double value and assign it to the variable.
Since one does rounding and the other just take the integer value, you see the difference.
Consider the following example:
double d = 1.99d;
int castedValue = (int) d; //1
int convertedValue = Convert.ToInt32(d); //2
In the above code, casting returned 1 since that is the integer part of the double value, whereas the conversion using Convert.ToInt32 rounded the value.
From the documentation from Convert.ToInt32(double):
Return Value
Type: System.Int32
value, rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.
From the C# 5 specification section 6.2.1, explicit numeric conversions:
For a conversion from float or double to an integral type [...]
[...]
Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.
(Emphasis mine.)
So basically, Convert.ToInt32 rounds up or down to the nearest int. Casting always rounds towards zero.

Unexpected double value in c# [duplicate]

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.
Today i come with a problem and not able to figure out what is the issue with this simple statement
I Tried
double d =1/4;
expected ans for me is 0.25 but in reality ans is 0.0 why so ??
And what should we do if statement is in terms of integer variables like this
double a =(a-b)/(d+e);
Because what you done is here integer division. 1 / 4 always give you 0 as a result regardless which type you assing it.
.NET has 3 type of division. From 7.7.2 Division operator
Integer division
Floating-point division
Decimal division
From Integer division part;
The division rounds the result towards zero, and the absolute value of
the result is the largest possible integer that is less than the
absolute value of the quotient of the two operands.
If you want to 0.25 as a result, you should define one of your values as a floating point.
You can use one of these;
double d = 1d / 4d;
double d = 1d / 4;
double d = 1 / 4d;
And what should we do if statement is in terms of integer variables
like this
double a =(a-b)/(d+e);
I assume your a, b, d and e are integers, you should use one of these then;
double a = (double)(a-b) / (double)(d+e);
double a = (a-b) / (double)(d+e);
double a = (double)(a-b) / (d+e);
double d =1d/4;
should work.
If you don't specify the type of your numbers, it is treated as Integer. And integer 1/4 will be zero.
Use this:
double d = (double) 1 / 4;
/ Operator (msdn)
When you divide two integers, the result is always an integer. For
example, the result of 7 / 3 is 2. To determine the remainder of 7 /
3, use the remainder operator (%). To obtain a quotient as a rational
number or fraction, give the dividend or divisor type float or type
double. You can assign the type implicitly if you express the dividend
or divisor as a decimal by putting a digit to the right side of the
decimal point.
Try this:
double d = 1.0 / 4.0;

Division in C# to get exact value [duplicate]

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.

Categories

Resources