C#: how to round an Integer to the nearest 1000 [duplicate] - c#

This question already has answers here:
How to round a integer to the close hundred?
(10 answers)
Closed 7 years ago.
How can i round an (int) so that a number like (22536) is equal to 22000 or 23000?
I haven't found a specific method in the Math class, Math.Round seems to round double only to the nearest int.

By using modulus:
int x = 1500;
int result = x % 1000 >= 500 ? x + 1000 - x % 1000 : x - x % 1000;
It checks if x has any more than 499 when the thousands are stripped, and then rounds it.

Related

Double multiplication with brackets problem [duplicate]

This question already has answers here:
1/252 = 0 in c#?
(3 answers)
Closed last year.
double celsius_0 = (100.5 - 32) * (5 / 9);
double celsius_1 = (100.5- 32) * 5 / 9;
Console.WriteLine(celsius_0);
Console.WriteLine(celsius_1);
output:
0
38,0555555555556
Why do they return differnt values?
I dont understand whats happening
It's because here celsius_0 = (100.5 - 32) * (5 / 9)
you count value in the first bracket then in the second one and then you multiply them.
While in the second line (100.5- 32) * 5 / 9 you count value in bracket then multiply it by 5 and at the end divide it by 9
To sum up in mathematics first you do things in brackets and then multiply/divide and finally add/substract

Converting division of int to int to a decimal is giving 0 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 4 years ago.
I came up with a formula to keep numbers a simple int and was surprised to see that the following was producing a 0.
var x = 45;
var y = 100;
var z = Convert.ToDecimal(x / y * 100);
In the above example, it almost seems ridicilous that I'm dividing by 100, then multiplying by 100 but in most cases, the x and y values are not nice integers.
When my x and y values end up being integers, the above conversion produces a 0. Why is that?
X / Y = 0.45
Int conversion of 0.45 is 0.
Therefore 0 * 100 = 0.
Therefore Convert.ToDecimal(0) is 0.
This does look a bit odd, but the problem there is with the order the expressions are evaluated: first the arguments of Convert are evaluated, and that is done inintegers. That’s why the result is truncated.
You should replace the conversation with an expression that forces evaluation in decimals:
var z = 100M * x / z;

floats being saved as 0 after calculation [duplicate]

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Closed 5 years ago.
Very confused here, I have this code:
float temp = (1/10)*100;
Label.Text = Convert.ToString(temp);
for some reason my temp variable is being saved as 0 which means my label text is changed to 0 when I'm expecting 10, I have the same issue when using doubles instead. What has gone wrong?
Since 1, 10 and 100 are all integer values, the division is also an int value, rounded down. In this case 1/10 = 0 so (1/10)*100 = 0
If you do not want this try using floats:
(1.0f/10)*100
In case you're working with integer variables you have to convert them first. This can be achieved by casting, like so:
int a=1;
...
float b = ((float) a)/10; // b will be 0.1
Another quick way of doing this in a line with multiple operations is multiplying by 1.0f:
int x = 100;
float c = (a*1.0f)/x; // c will be 0.01f

C# Using Bool, how to check a double is truly divisible by another number? [duplicate]

This question already has answers here:
How can I calculate divide and modulo for integers in C#?
(5 answers)
Closed 7 years ago.
Is there a way of checking that a value is wholly divisible by another number, for example 1000 divided by 100 would be true, but 1115 divided by 100 would be false?
I am tring to
Any help would be much appreciated :-)
You can use the %-operator:
bool isDivisible = 1115 % 100 == 0;
The % operator computes the remainder after dividing its first operand
by its second. All numeric types have predefined remainder operators.
Check out % operator. 1000 % 100 yields 0. 1115 % 100 yields 15
You can use mod operator (%) and check that remainder is equal to 0:
var result = (1000.00 % 100) == 0; // evaluates to true
var result = (1115.00 % 100) == 0; // evaluates to false

C# - Having trouble dividing by a 0.x number [duplicate]

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.
My C# program requires two numerical outputs.
A main price will be divided by 20% and 10%.
For example:
Main price = 200
Discounted price: 20% of 200 = 40. Therefore total price is now 160.
I am having issues converting decimals, int's, etc. Here is what I have tried.
int discountNumber = 20 / 100;
decimal DiscountedPrice = Convert.ToInt32(TotalPrice)
/ Convert.ToInt32(discountNumber);
txtTotalPrice.Text = DiscountedPrice.ToString();
The problem in the
int discountNumber = 20 / 100;
it's always zero
change the int to a double. 20/100 will give you a decimal figure not an int

Categories

Resources