Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Decimals in XYZ-point

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
flerekilosten
727 Views, 4 Replies

Decimals in XYZ-point

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?

Tags (3)
4 REPLIES 4
Message 2 of 5

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 🙂

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 5

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?

Screenshot_1.png

 

Kind regards
Mathias

Message 4 of 5

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: 

  

  • Send it as a number to Excel and let Excel format it
  • Send it as a string to Excel and control the formatting yourself

 

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:

 

https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/Util.cs#L1446-L...

 

 

  /// <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

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 5 of 5

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.

Post to forums  

Rail Community


Autodesk Design & Make Report