Possible Duplicate:
How to round and format a decimal correctly?
I have this:
double xValue = 0.0;
double yValue = 0.0;
foreach (var line in someList)
{
xValue = Math.Round(Convert.ToDouble(xDisplacement - xOrigin), 2);
yValue= Math.Round(Convert.ToDouble(yDisplacement + yOrigin), 2);
sw.WriteLine("( {0}, {1} )", xValue, yValue);
}
When it does that math it is suppose to round to 2 decimal places.
..When a number is something like 6.397 it will round it to 6.4 and not include the trailing "0".
How can I add the "0" at the end of the number?
If I add this to BEFORE (unless there is a better way to do it?) the foreach loop above...:
string properX = xValue.ToString().Replace(".", "");
string properY = yValue.ToString().Replace(".", "");
How would I go about doing this?
Use a format string:
sw.WriteLine("( {0:0.00}, {1:0.00} )", xValue, yValue);
For documentation, look at Standard Numeric Format Strings. String.Format and TextWriter.WriteLine expose the same format options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With