Hi
I'm trying to round an XYZ point to 2 decimals for future export to an Excel sheet using the code below.
public static XYZ XYZRound(XYZ elem)
{
XYZ Rounded = new XYZ(Math.Round(elem.X, 2),
Math.Round(elem.Y, 2),
Math.Round(elem.Z, 2));
return Rounded;
}
However, when using the .ToString() command on the rounded number this is reverted to the set number of decimals (9).
I've tried manually constructing a formatter as seen below but this caused problems in Excel as it overrides user defined decimal separators.
string.Format("({0}, {1}, {2})", Math.Round(((XYZ)obj).X, 2).ToString(), Math.Round(((XYZ)obj).Y, 2).ToString(), Math.Round(((XYZ)obj).Z, 2).ToString())
Is there any way to circumvent the .ToString() from adding additional decimals?
Solved! Go to Solution.
Solved by jeremy_tammik. Go to Solution.
Yes.
Please read the .NET ToString documentation:
https://duckduckgo.com/?q=.net+tostring+format
As a a general hint, I would recommend always searching the Internet for answers and reading some documentation before asking a question. It will save you mountains of time and the rest of the world mountains of effort. You will learn things, appreciate the work people have put into documentation, and the world will be an even better place than it already is 🙂
Hi Jeremy
Thank you very much. I had searched around beforehand but had trouble translating this to XYZ points.
The ToString() override for XYZ does not seem to accept any of overloads (also as far as I can see in the metadata).
How would you input the additional arguments?
Kind regards
Mathias
Dear Mathias,
Ah, yes, indeed, the XYZ built-in ToString method does not give you any formatting control.
Therefore, I would not use it at all.
Afaik, Excel does not understand or accept XYZ objects, so you have to handle and send the three coordinates separately anyway, don't you? So, XYZ is not really the question here at all; the question is, how to transport a double number to Excel.
Here you have two options:
You cannot do both at the same time, send it as a number and retain formatting control.
Maybe The Building Coder samples XYZ to string formatting approach will be of use to you:
/// <summary>
/// Return a string for a real number
/// formatted to two decimal places.
/// </summary>
public static string RealString(double a)
{
return a.ToString("0.##");
}
/// <summary>
/// Return a string for a UV point
/// or vector with its coordinates
/// formatted to two decimal places.
/// </summary>
public static string PointString(
UV p,
bool onlySpaceSeparator = false)
{
var format_string = onlySpaceSeparator
? "{0} {1}"
: "({0},{1})";
return string.Format(format_string,
RealString(p.U),
RealString(p.V));
}
/// <summary>
/// Return a string for an XYZ point
/// or vector with its coordinates
/// formatted to two decimal places.
/// </summary>
public static string PointString(
XYZ p,
bool onlySpaceSeparator = false)
{
var format_string = onlySpaceSeparator
? "{0} {1} {2}"
: "({0},{1},{2})";
return string.Format(format_string,
RealString(p.X),
RealString(p.Y),
RealString(p.Z));
}
Cheers,
Jeremy
Hi Jeremy
I'll try one of those approaches. Thank you!
Kind regards
Mathias
Can't find what you're looking for? Ask the community or share your knowledge.