Not bring able to figure out how to format numbers like I want is starting to pile up on me and really trip me up. Consider these following 3 examples where I am currently stuck:
Example 1 (the situation I've been describing in this thread):
I am printing a report of component heights (reading PACKAGE_HEIGHT_MAX). As the components could be built in various units, and I want to convert to a single unit system (in my case MILS), I will end up with heights in various formats. Footprints built in MILS will likely be integers, but footprints built in MM will almost certainly be floating point numbers, as will footprints built in INCHES (for example, 2 INCHES = 2.000 MILS).
To make the report much cleaner, I'd like footprints built in MILS to appear as integers. Footprints in MM and INCHES will almost certainly be fp numbers, but I'd prefer not to have trailing zeros.
Question: How can I take a variable that is either an integer or fp number and format it without any trailing zeros so it can be printed as a number?
I want to accept user input and print it back to the user. As the design could be done in MILS, MM, or INCHES, the value the user inputs could be an integer or fp.
Furthermore, I when I print the input back to the user, I want to append the units. So I need to take the number (either integer or fp) and make it a string then append the units. Now, I can use sprintf to make the number a string, and strcat to tie the number and units together, if I could just get the number properly formatted. It looks silly when the user puts in "2" (for example, in INCHES) and gets back "2.000 INCHES". If I was the user, I'd expect "2 INCHES". Similarly, if the user puts in "25" in MILS, you don't want to get back "25.000 MILS" since the trailing zeros are so small they're generally irrelevant.
Question: How can I accept user input that is either integer or fp, and get that into a string without any trailing zeros (note similarity to above)?
I am creating a report in which one column is a property that is defined by the footprint symbol, but which is brought from the schematic ("Create user-defined properties" must be checked in the Export Physical form to annotate this property to Allegro). This property, the distance between through holes, is not present on SMT or irrelevant parts (property is "nil") and the property is "NA" for through hole parts where the pad spacing doesn't matter. Where pad spacing does matter, it is a fp number (for example, ".200" in MILS). I am using Extracta to get this property into my SKILL routine and then nth(...) to pull out the desired value from the Extracta output file.
So, when the report is printed I am using "%L" format specification to be able to print either "nil", "NA", or a fp number without any errors but I am having problems with the resulting data. When the property doesn't exist, I get "nil" without quotes. When the property value is "NA" (without quotes), I get "NA" with quotes, which I assume is because this is a two-letter string. And if there is a number, I get the number surrounded by quotes.
The program that needs this report cannot accept the quotes, so I must first remove the quotes. But how? Also, I'd prefer to have nothing in the report column if the property doesn't exist (or if it's "NA"), and (ideally) the only condition that would place something in this column would be if there is a value for this property that is not "NA" (without quotes). I can visualize how to do this, but I can't seem to figure out the right way to make it happen. I am able to detect "NA" as the value, but I'm not able to change it to "nil". I get a "left hand assignment error" when I try to set the nth(...) variable equal to "" (doube quotes) or "nil".
Question: How can I accept a variable with a value of nothing (nil), a string, or a fp number, and print the variable without any errors? Bonus points if I can wipe out the value of the variable (make it nil) if it meets my criteria.
Wow, I am long winded! Thank you if you could trudge through all that. I'm sure it's a bit harder to figure out without seeing the code, but I have to believe this is a fairly common problem. Ever since the first programs were written, I can imagine similar situations. There has to be a way to achieve the formatting I desire or else programs I regularly use wouldn't display numbers the way they do. I know this type of formatting can be done.
So, in short, if I have a variable that is an integer or fp number, how can I print it without trailing zeros?
The second part of the question is similar to the first: if the variable may not have any value, or might be a string, or might be a fp number, how can I accept any of those and print out (optionally discarding strings and nil values) only the fp number, so I get nothing if the value of the variable is nothing?