| It seems that there's a sporadic fault in the @Round function in Xpages SSJS. I have a piece of code that calculates a number and is supposed to display the value, rounded to two decimal places in a tooltip on the page. We rounded the number with @Round(value,0.01), and it worked just fine in testing - rounded all the values we threw at it as expected. But our customer reported sporadic incidents where the rounding clearly wasn't working. So I did some digging - and here's what I found. The calculated number to be rounded to 2DP was 26.811000000000003 (that's 11 zeros). That's a value I found generated the issue in testing - there are other values we've seen reported that don't round properly, and all of them have a long string of zeros before a non-zero digit at the right end. Some quick testing showed me that
Some Googling suggested that @Round has been under suspicion before. eOffice's blog has a post describing using Math.Round instead of @Round for some other purpose. But Math.Round rounds at the decimal point only. Julian Buss though showed how to move the rounding point about - so simple! And that gave me the idea for the workround I used, which just moves the effective digit that @Round works at to somewhere where it appears to work. My workround was this: @Round(value*100,1)/100: which works fine - or at least it does for 26.811000000000003. But rounding to two decimal places is probably the commonest use of rounding, so the fact that there's an issue here is really is a bit of a worry. |
Comments (5)
Mick Moignard November 20th, 2009 04:17:10 AM
Have you tried the "traditional" @round? I believe that if you call an @function inside session.evaluate() you get the normal @function and not the one written for SSJS. I've not tried @Round this way but @Unique is clearly different.
I think you can better use Math.round(value) in SSJS.
I found in the Tagcloud who is in OpenNTF Xpage templates, the same problem.
When I changed the @Round to Math.round it was fixed.
see { Link }
@3: Frank - that's the very eOffice post I mentioned....
@1 and @2: yes, and I considered Math.round, but the real issue is that the SSJS @Round doesn't work properly.
@4: in Javascript there is for a Number two methods, toFixed() and toPrecision().
I have used toFixed() recently to manage the display of a number value.
See { Link }