No NaN for me please
By Tim Dexter on Feb 07, 2010
Being British, I of course love Indian food, its almost our national dish back in the UK. Roast beef and yorkshire pud is no longer the go to faire. Its a spicy hot chicken Pathia or Dal for me all the way. Thats sometimes hard to find here in Colorado, we have taken to making our own having found only a handful of Indian restaurants in Denver and Springs (suggestions welcome locals :0) Nan bread is now available in local supermarket so we have not built our own tandoori oven.
My tenuous bog post title is not referring to that delicious bread but to an XSL and therefore Publisher issue, well maybe be not an issue; more of something you may need to handle.
there is no native support to handle divide by zero errors or maybe mistakenly dividing a string into a number. Right now the only way around it is to check every value prior to using it in a calculation e.g.
<?if: XXXX >0?> <?XXXX div YYYY?><?end if?>
Not so bad, but if you have multiple calculations to handle in a template it can become a pain and of course is a bunch of code to write or at best copy and paste.
There was a questions on the forum this week asking if BIP had a safeDivide function like Actuate has? After a little digging to find out what the heck a safedivide function was, my initial thought was to create a template(function) for the template to call prior to any divide functions. I am from the school of 'why re-invent the wheel' so Google it took another hit. Lots of cool solutions out there; one of my favorite sites is from Dave Pawson of XSLT book fame - http://www.dpawson.co.uk/xsl/sect2/sect21.html. There is a mass of answers on Dave's site from a host of experts, well worth bookmarking.
Dave's site popped up this solution
Define a format name, like so:
<xsl:decimal-format name="MyFormat" NaN=" - " zero-digit="0"/>
And use it with your format statement:
this is much simpler than building a function. The first statement needs to be placed at the top of your template. In this case if a divide by zero occurs i.e. a NaN then a '-' will be returned.
First thought, corrected and a solution found. My second thought, we need to provide something natively to handle this for folks. Some folks have differing requirements in separate parts of an output, maybe 'N/A' in sme cases and '0' for others. So, I logged an enhancement request to handle this. I logged 9355897, Template Meister Hok Min picked it up and implemented the enhancement within the day. Now waiting for a release vehicle but there's service for you, logged, coded and tested within the day. If you are desperate for it, you can get support to log a one off request on your behalf.