Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double 2 Decimal Places [duplicate]

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.

HOWEVER,

..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?

like image 390
theNoobGuy Avatar asked Nov 25 '25 09:11

theNoobGuy


1 Answers

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.

like image 165
jason Avatar answered Nov 26 '25 23:11

jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!