Two Decimal places using c#
If you want to round the decimal, look at Math.Round()
How do I display a decimal value to 2 decimal places?
decimalVar.ToString("#.##"); // returns ".5" when decimalVar == 0.5m
or
decimalVar.ToString("0.##"); // returns "0.5" when decimalVar == 0.5m
or
decimalVar.ToString("0.00"); // returns "0.50" when decimalVar == 0.5m
Rounding a variable to two decimal places C#
Use Math.Round and specify the number of decimal places.
Math.Round(pay,2);
Math.Round Method (Double, Int32)
Rounds a double-precision floating-point value to a specified number
of fractional digits.
Or Math.Round Method (Decimal, Int32)
Rounds a decimal value to a specified number of fractional digits.
Using String Format to show decimal up to 2 places or simple integer
An inelegant way would be:
var my = DoFormat(123.0);
With DoFormat
being something like:
public static string DoFormat( double myNumber )
{
var s = string.Format("{0:0.00}", myNumber);
if ( s.EndsWith("00") )
{
return ((int)myNumber).ToString();
}
else
{
return s;
}
}
Not elegant but working for me in similar situations in some projects.
Round double in two decimal places in C#?
This works:
inputValue = Math.Round(inputValue, 2);
Formatting a double to two decimal places
string.Format
will not change the original value, but it will return a formatted string. For example:
Console.WriteLine("Earnings this week: {0:0.00}", answer);
Note: Console.WriteLine
allows inline string formatting. The above is equivalent to:
Console.WriteLine("Earnings this week: " + string.Format("{0:0.00}", answer));
Display two decimal places for a string value
You'll probably want to try something like:
double get_amt = 0;
if (dt_.Rows[i]["billing_amt"] != DBNull.Value)
get_amt = Convert.ToDouble(dt_.Rows[i]["billing_amt"]);
Then:
string.Format("{0:0.00}", get_amt)
should work.
It's currently not working as it's a string value your trying to format - which wont have decimal places.
C# Round Up Two Decimal Places in C#?
Try Ceiling method:
- Scale the
value
up:6.3619 -> 636.19
- Truncate with a help of
Math.Ceiling
:636.19 -> 637
- Finally, scale the result down:
637 -> 6.37
Code:
var result = Math.Ceiling(value * 100.0) / 100.0;
Demo:
double[] tests = new double[] {
6.3619,
5.12003,
};
string report = string.Join(Environment.NewLine, tests
.Select(test => $"{test,10} -> {Math.Ceiling(test * 100) / 100.0}"));
Console.Write(report);
Outcome:
6.3619 -> 6.37
5.12003 -> 5.13
Related Topics
In C# Wpf, Why Is My Tabcontrol's Selectionchanged Event Firing Too Often
How to Ensure Entries with Non-Overlapping Time Ranges
Reading Data from CSV to Screen Output
JSON Serialize List<Keyvaluepair<String, Object>>
How to Compare Two Objects in Unit Test
How to Convert Code from C# to PHP
How to Rewrite Complicated Lines of C++ Code (Nested Ternary Operator)
Render PDF in Itextsharp from HTML with CSS
When Should I Use Out Parameters
Publish .Net Core App as Portable Executable
Sending a Value from Server to Client with Sockets
ASP.NET Core Disable Authentication in Development Environment