This question already has answers here:
Round up to next quarter
(3 answers)
Closed 5 years ago.
Any way to round up decimal value to its next quarter (0.25,0.50,0.75) in C#.Net?
I am getting wrong rounding with Math.Floor function
Example:
5.125 -> 5.25
6.390 -> 6.50
7.610 -> 7.75
8.950 -> 9.00
decimal UltimateRounding(decimal amountToRound, decimal nearstOf, decimal
fairness, decimal final)
{
return Math.Floor(amountToRound / nearstOf + fairness + final) * nearstOf;
}
Try
var a = 5.125m;
a = Math.Ceiling(a * 4) / 4;
Related
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 2 years ago.
I have the amount
var Amount = 23454;
And I need to Format it like this: 234.54
I searched for similar problems and tried this:
String.Format("{0:0.00}", Amount / 100) // 234.00
String.Format("{0:0.##}", Amount / 100) // 234
(Amount / 100).ToString(CultureInfo.CreateSpecificCulture("en-GB")) // 234
but it removes decimals
This should do the trick:
String.Format("{0:0.##}", ((Decimal)Amount) / 100)
As mentioned before, you are working with an integer and not a decimal. You have to cast it before doing your logic
This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 5 years ago.
I want to limit decimal place till 4 for below code, how to do that,
var a = Convert.ToDecimal( 80794992640) / (1024 * 1024);
I guess you can round it if you want to fix it to 4 digits:
var a = Math.Round(Convert.ToDecimal( 80794992640) / (1024 * 1024),4 );
but if your concern is to restrict it in the display then you can just apply the restriction in the ToString method:
a.ToString("0.####");
the latter method will keep the precision for the calculations but cut the precision only for display
This will round it to 4 decimal places:
var a = Math.Round(Convert.ToDecimal( 80794992640) / (1024 * 1024), 4);
This question already has answers here:
Division returns zero
(8 answers)
Closed 5 years ago.
Im trying to get 75% of the CTotal but as it is a decimal number it is rounding 0.75 to 0, does anyone know of a work around
decimal refundtot = order.CTotal;
//change it as it is making it = 0
refundtot = (75 / 100) * refundtot;
refund.RefundTotal = refundtot;
You should use one decimal number when you dividing numbers.
Your code should look like this :
efundtot = ((decimal)75 / 100) * refundtot;
This question already has answers here:
Division returns zero
(8 answers)
Closed 8 years ago.
I'm trying to get Celsius to take the Fahrenheit temperature as an argument, then return the temperature in Celsius
class Exercise1
{
static void Main(string[] args)
{
double fahrenheit;
Console.WriteLine("Enter farenheit: ");
fahrenheit = double.Parse(Console.ReadLine());
double cels;
Exercise1 me = new Exercise1();
cels = me.Celsius(fahrenheit);
Console.WriteLine(cels);
}
public double Celsius(double fahrenheit)
{
double celsius;
celsius = 5 / 9 * (fahrenheit - 32);
return celsius;
}
Your problem is integer division in your Celsius function. This line
celsius = 5 / 9 * (fahrenheit - 32);
Will always be 0 because 5 / 9 will be divided as integers which will always give you 0. To force floating-point division, one if your integers must be a double. So do this:
celsius = 5.0 / 9 * (fahrenheit - 32);
Note the 5.0 will force floating-point division.
5 / 9 would be seen as integers and thus make the entire calculation integer maths in this case.
Meaning you are essentially getting 0 * (fahrenheit - 32).
Cast either or both 5 and 9 to a double to force floating point maths.
This question already has answers here:
How might I convert a double to the nearest integer value?
(8 answers)
Closed 9 years ago.
I'am producing decimal such as:
0.8235294117647058823529411765
0.1764705882352941176470588235
I'd like to multiply them by 10 then round them. If the second number after the dot is less than 5 then make it 0. Otherwise, make it 1. For the above examples, that would be:
8
2
The result should be put in a int.
I thought it is simple.
Math.Round(10 * your_decimal);
Reference: http://msdn.microsoft.com/en-us/library/3s2d3xkk.aspx
You can use Math.Round() if you want to keep it as a decimal or round to a certain precision.
decimal dec = 0.8235294117647058823529411765m;
decimal rounded = Math.Round(dec * 10); // 8m
decimal roundedToOne = Math.Round(dec * 10, 1); // 8.2m
FYI, an explicit conversion is defined for decimal to int so you can round down by casting to an int
int a = (int)(dec * 10); // 8
This could be combined with a condition to round the number up and down
decimal num = dec * 10;
int a = (int)num + (num % 0 < .5m ? 0 : 1); // 8