How to restrict calculated value to four decimal places [duplicate] - c#

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);

Related

Using String Format from int to string double and keeping decimal C# [duplicate]

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

The result of calculation deviates after some digits [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Result of calculation shown below should be equal to 0,007306897 (I referred from 2 books) . But when i check result from Watch 1, i see that result is equal to 0,007306882. I split the process into some parts. And the problem is occurring when c is calculating.
///Declarations
double sigma = 1.00000000;
double a,b,e,c;
a = (1 / Math.Sqrt(2 * Math.PI)); //calculated properly
c = -(i * i + j * j) / 2.00000000 * (sigma * sigma); //i and j are equal to -2
e = Math.E; //calculated properly
b = Math.Pow(e, c);
result=a * b;
Unfortunately the double type is not highly accurate. The link below shows that some numbers like 1.05 can not be stored accurately by the double type.
http://www.binaryconvert.com/convert_double.html
For normal usage it is usually accurate enough, if you need accuracy to the level you describe you probably need to use something like binary coded decimal. That will mean the normal math library won't work. You could try asking on some physics sites to see what they use when modelling complex systems to maintain accuracy.

how to round decimal value to next quarter [duplicate]

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;

multiplying a decimal by a floating point number [duplicate]

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;

How to round decimal to an int in c# [duplicate]

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

Categories