This question already has answers here:
Two Decimal places using c#
(13 answers)
Closed 5 years ago.
I am trying to formatting decimal number in c#.I tried ToString("N") and String.Format but it does not work because Total is defined as a decimal in the database.For example, If the total is 93.7567, then it should be 93.76. Here is the code:
Total = g.Sum(c => c.Total)
You could use Math.Round() Method.
total = Math.Round(total,2); // 93.7567 becomes 93.76.
You can try decimalvalue.ToString("#.##"); or decimalvalue.ToString("F")
Related
This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 5 years ago.
so I have the following problem I'm trying to output 2 numbers after the decimal comma but it doesn't output it with 2 numbers only when I have zero or more than one zero at the end of the number.
Console.WriteLine(Math.Round(s * 0.07, 2));
here's how I did it.
You might want:
Console.WriteLine($"{s:N2}");
This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Closed 6 years ago.
I have a small question regarding Math.Round function.
I need the string "12.123456" to be rounded at 4 decimals. I used:
Math.Round(Convert.ToDouble(pData), 4).ToString()
where pData is defined as string, but the values are decimal with 7 decimals.
My problem is that I expected to get every time the exact 4 decimals, but for some values it gives me only 2 (eg. 12.12 instead of 12.1200).
How can I change in order to always get the needed 4 decimals?
Regards,
You should use format strings instead:
pDate.ToString("0.0000")
or
pDate.ToString("n4")
This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 7 years ago.
I need 45.7 to be 45.70
Math.Round(d, 2) have no effect.
Have tried with decimal and double type.
I would appreciate any kind of help.
You need to apply a string format:
string.Format("{0:N2}", 45.7)
Check here for docs on the string formatters
This question already has answers here:
How to find an average date/time in the array of DateTime values
(7 answers)
Closed 8 years ago.
I need to calculate the average the following observed dates:
1/7/2010
15/7/2011
17/6/2012
3/7/2013
How can I do that in asp.net or any other language. If there is any formula to do that.
You need to convert it to Ticks (this will convert it to System.Int64) and then do the average of them all.
See the following answer: https://stackoverflow.com/a/16683441/643761
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
I have a number
long n = 32432432423;
I want to divide this by 1450 and print on a console with 2 decimal places (rounded)
How can I do it?
COnsole.WriteLine(????);
Console.WriteLine("{0:N2}", ((double)n) / 1450);
Console.WriteLine("{0:0.00}", 32432432423 / 1450.0);
make use of Math.Round
Console.WriteLine( Math.Round(Convert.ToDecimal(32432432423 / 1450.0), 2));
Result: 22367194.77