3

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?

Community
  • 1
  • 1
theNoobGuy
  • 1,636
  • 6
  • 29
  • 45
  • Unrelated to your question but, do you really want `xDisplacement - xOrigin`? I've no idea what your application does but normally a displacement is subtracted from the origin, not the other way around. – Daniel Renshaw Aug 26 '11 at 15:50
  • @Daniel: Haha I know it is a bit confusing. But I actually am figuring out something different with it.. this was just a general question :P – theNoobGuy Aug 26 '11 at 15:52
  • http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.71%29.aspx – Freelancer May 30 '13 at 12:21

5 Answers5

5

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.

jason
  • 236,483
  • 35
  • 423
  • 525
  • Hard-coded decimal point could cause issues with L10N – stuartd Aug 26 '11 at 15:48
  • 2
    @Stuart Dunkeld: No! That is not how the `.` specifier works in a format string. The localization settings are taken into consideration by String.Format so that the appropriate symbol is used. cf. http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierPt where it states "The "`.`" custom format specifier inserts a localized decimal separator into the result string." and "The character that is used as the decimal separator in the result string is not always a period; it is determined by the `NumberDecimalSeparator` property of the `NumberFormatInfo` object that controls formatting." – jason Aug 26 '11 at 15:54
3

The first set of examples in this link:

http://www.csharp-examples.net/string-format-double/

// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

Should be what you are looking for. It also details lots more.

MSDN documentation on string numeric formatting:

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierPt

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Sorry, but this allows to persist a myth that using `.` does not support localization. Note that the documation for [The "`.`" Custom Specifier](http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierPt) states: "The character that is used as the decimal separator in the result string is not always a period; it is determined by the `NumberDecimalSeparator` property of the `NumberFormatInfo` object that controls formatting." – jason Aug 26 '11 at 15:57
  • @Jason fair enough, I wasn't sure if it supported localisation, hence I chose my words carefully and said "you may need". But I've removed it anyway and provided a link to the MSDN docs you mentioned. – Adam Houldsworth Aug 26 '11 at 15:59
  • Fair enough. It's such a common myth that even though you weren't firm in your statement, I had to comment. – jason Aug 26 '11 at 16:01
2

This trailing zero has nothing to do with the number itself, but rather with its representation. So if you want to display it, there is nothing to change with the number - use string formats:

sw.WriteLine("( {0:0.00}, {1:0.00} )", xValue, yValue);
Andrei
  • 55,890
  • 9
  • 87
  • 108
1

Use a numeric format string to display as many decimal places as you like:

sw.WriteLine("( {0:N2}, {1:N2} )", xValue, yValue);
stuartd
  • 70,509
  • 14
  • 132
  • 163
1

Rather than round the value, which you may not want to do, you can do the rounding just as part of the output formatting:

WriteLine("{0:0.00}", value)
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490