Community
Participate
Working Groups
Created attachment 94810 [details] Product Sales by Country.rptdesign Open the attached report (or build your own.) Look at the Change column for a country, say Australia. This field has a value computed using the Finance.percent( ) function provided by BIRT. The result is a percent. That is 100 percent means 1 (number). Finance.percent( 2, 1 ) returns 50. (Note the strange reversal of numerator and denominator: one thinks of division as num/denom, but the percent function uses percent( denom, num ). Hard to remember, but it is too late to change.) Now, look at the style for this item. It uses BIRT's percent formatting. Preview the report. The field should show the percentage change as a percent. But, it shows 100 times the change. The reason appears to be that the percentage formatting feature assumes percents are represented as ratios: .5 for 50% and so on. The result is that one can choose to use one or the other feature, but not both together. This gets tricky because: * One cannot specify a custom format so I can do my own percentage. (See Bug 225692.) * I used percent because I need a "safe divide", one that handles a zero in the denominator. I assumed that Finance.percent handled this case. The workaround is to use percent formatting (since I can't easily do custom formatting without writing messy code), and to do a roll-my-own safe divide: if ( denom == 0 ) then 0; else num / denom; A suggestion for resolution is to add a new method: BirtComp.safeDivide( num, denom [, if0 ] ) Example: BirtComp.safeDivide( 10 /* change */, 0 /* base */, 100 ); Here, I want to treat an increase from zero as a 100% increase. (Not mathematically correct, but a useful approximation for business people.) BirtComp.safeDivide( a, b, null ); Here I want a null (undefined) value if b is 0.
In current implementation, if denom is null, the IllegalArgumentException is thrown. Do we expect 0 if the denom is null? It is OK to support BirtComp.safeDivide( num, denom [, if0 ] ) in the report part. Any comment from Data team? BTW, the design file cannot be previewed correctly in BIRT 2.3. Please refer to the bug 226409.
Created attachment 95489 [details] Revised Product Sales by Country.rptdesign. Can see data in BIRT 2.3
(In reply to comment #1) > In current implementation, if denom is null, the IllegalArgumentException is > thrown. Do we expect 0 if the denom is null? FWIW, my suggestion is to not change the current function to avoid breaking existing code. You could, if you wanted, add an optional third argument. If the third argument exists, return that for a divide-by-zero. > BTW, the design file cannot be previewed correctly in BIRT 2.3. Please refer to > the bug 226409. I'm using BIRT 2.2. If the change to 2.3 breaks designs, some users may be a bit upset...
SafeDivide function is available in the BirtMath object. It will be made available after BUG 227644 is resolved. I think the consensus is to leave the behavior of Finance.percent() unchanged to preserve backward compatibility.
See dependency for bug 227644.
Uses BirtMath.safeDivide( ). This is now available on the BIRT expression builder. Details refer to depend bugs.
verified.