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: 

Unit converted parameter value not matching parsed string value

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
floretti
193 Views, 4 Replies

Unit converted parameter value not matching parsed string value

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

 

 

 

4 REPLIES 4
Message 2 of 5
jeremy_tammik
in reply to: floretti

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
Message 3 of 5
floretti
in reply to: floretti

@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.

Message 4 of 5
jeremy_tammik
in reply to: floretti

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
Message 5 of 5
floretti
in reply to: floretti

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community