Unit Conversion and Rounding in Revit API

Unit Conversion and Rounding in Revit API

danail.momchilov
Contributor Contributor
332 Views
3 Replies
Message 1 of 4

Unit Conversion and Rounding in Revit API

danail.momchilov
Contributor
Contributor

Hi!

I'm trying to get areas in square meters. The problem is they need to be always rounded in the exact same way Revit would round them to a precision of 0.01 

Here's what I tried:

double valueMeters = UnitUtils.ConvertFromInternalUnits(valueFeet, UnitTypeId.SquareMeters);

string valueMetersRounded = UnitFormatUtils.Format(doc.GetUnits(), SpecTypeId.Area, valueMeters, false);


The problem is that way I would always get the final formatting in accordance with the unit precision set for the project units, which might not always be 0.01

How would one set the precision to be always down to the 0.01, using only the built in API methods? here's a more detailed breakdown of the issue:

danailmomchilov_0-1744729768029.pngdanailmomchilov_1-1744729794198.png

It's clearly visible Revit's rounding is inconsistent in some specific border cases, but I need precisely identical results with the ones, visualized in the UI. When using

Math.Round(value, 2, MidpointRounding.AwayFromZero);


All of the specified values will be rounded to 12.12 without exception. This would also be the case with 

MidpointRounding.ToEven

etc. So one would always end up with a tiny difference of 0.01 m² in some cases. 

This seems like a trivial issue, but is really causing a lot of trouble, e.g. when automating precise area calculations for residential buildings.

Any suggestions would be highly appreciated 🙏

 

0 Likes
Accepted solutions (1)
333 Views
3 Replies
Replies (3)
Message 2 of 4

TripleM-Dev.net
Advisor
Advisor

Hi @danail.momchilov ,

 

Not clear what's what. In the images what represents the cases which is correct and which not?

 

From the post, "Math.Round(value, 2, MidpointRounding.AwayFromZero);" does it as you want, but you aren't allowed to use this?

 

Also, what you see in the UI depends on a lot of things.

If it's what the tag shows, well the tag can have a different rounding set than the project, same with schedules.

Internally Revit uses the non-rounded values, so if you would sum rounded values you get a different sum in the UI vs API.

 

Advise really depends on why that rounding is needed and how it's further used.

Do you need to conform to some standard/ISO or so?

I always keep non-rounded values for behind the scene calculations (and mostly in Revit API units) and show rounded values in my own UI (form / database etc)

 

- Michel

0 Likes
Message 3 of 4

danail.momchilov
Contributor
Contributor

Hi! 

 

In the images you see the same areas shown with different precision set in the project units. What one can see is some of them are rounded to 12.12 while others to 12.13, while regular mathematical rounding should return 12.12 in all of these cases. This is why I cannot use it. I simply need the exact values Revit returns when the precision is set to 0.01. I am exporting these values to excel and also use them for additional calculations and need them all to perfectly correspond to Revit schedules and drawings. There is no additional formating in these examples (coming from tags, etc) other than Revit internal formatting.

 

Cheers

0 Likes
Message 4 of 4

danail.momchilov
Contributor
Contributor
Accepted solution

I found it !

This seems to do exactly what I need:

            public string sqFeetToSqMeters(double valueFeet)
            {
                FormatOptions customFormat = new FormatOptions(UnitTypeId.SquareMeters);
                customFormat.Accuracy = 0.01;
                customFormat.AUseDefault = false;

                FormatValueOptions formatOptions = new FormatValueOptions();
                formatOptions.AppendUnitSymbol = false;

                formatOptions.SetFormatOptions(customFormat);

                return UnitFormatUtils.Format(doc.GetUnits(), SpecTypeId.Area, valueFeet, true, formatOptions);
            }