Unit converted parameter value not matching parsed string value

Unit converted parameter value not matching parsed string value

floretti
Advocate Advocate
558 Views
4 Replies
Message 1 of 5

Unit converted parameter value not matching parsed string value

floretti
Advocate
Advocate

Hi all,

 

Thanks for your time first of all. Strictly speaking this is not a Revit API issue I'm facing but after some research I couldn't find the answer here or anywhere else but it's still a challenge related to Revit.

 

I'm comparing a family parameter value with values in a spreadsheet so I can update the parameter accordingly if they don't match but I'm getting weird results when comparing them. The snippet below shows how I'm extracting the double value of the WW_Width parameter and converting it to millimetres. Then I compare it to the a parsed string with the exact same value as a double but I get a false result to whether or not they match.

 

 

 

var famPars = doc.FamilyManager.Parameters;
var famTypes = doc.FamilyManager.Types;

foreach (FamilyParameter param in famPars)
{
	foreach (FamilyType famtype in famTypes)
	{
		if (param.Definition.Name == "WW_Width")
		{
			console.ShowBoldMessage("WW_Width");
			console.ShowMessage($"UnitType: {param.Definition.UnitType}");
			console.ShowMessage($"StorageType: {param.StorageType}\n");

			double valueInMM = UnitUtils.ConvertFromInternalUnits((double)famtype.AsDouble(param),
																	DisplayUnitType.DUT_MILLIMETERS);
			double parsedString = double.Parse("2448");

			console.ShowMessage($"Value: {valueInMM}");
			console.ShowMessage($"Parsed string: {parsedString}");
			console.ShowMessage($"Values match: {valueInMM == parsedString}");
		}
	}
}

 

 

 

This is what my console shows as a result but I don't get why I'm getting a mismatch.

 

 

WW_Width
UnitType: UT_Length
StorageType: Double

Value: 2448
Parsed string: 2448
Values match: False

 

 

 

0 Likes
Accepted solutions (1)
559 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni
Accepted solution

You need to add some fuzz:

  

https://www.google.com/search?q=fuzz&as_sitesearch=thebuildingcoder.typepad.com

  

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

floretti
Advocate
Advocate

@jeremy_tammik Thanks for the steer in the right direction. I wasn't aware of that issue with doubles/floats. Instead of directly comparing the doubles, I'm subtracting one from the other and checking if the difference is under a certain tolerance (e.g. A - B < 0.001) and that works.

 

This article explains it well in case anyone faces this issue in the future.

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni

Brilliant! Glad that it helped, and thank you for the appreciation and nice helpful pointer.

  

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

floretti
Advocate
Advocate

I'm always very thankful to these forums and the help I get here as I have no one around me to ask these questions to when I'm stuck.

0 Likes