Bug report in handling fn:format-number in basex 8.4.4
Friday, 3 June 2016 · 3 messages
MS
Michael Stoop3 Jun 2016 09:26 #
Good morning,
First of all thank you for your great work on basex. I've been phasing out Zorba in favour of BaseX for excellent large file support and all around better documentation and support.
However, I'm running into some issues in formatting numbers in basex 8.4.4 using
fn:format-number (using http://www.w3schools.com/xsl/func_formatnumber.asp)Quoted text · 1 line
To describe this behavior I wrote a simple xquery:
let $result :=
<rows>
<row input="0.3576" format="0.##" expected="0.36">{fn:format-number(0.3576, "0.##")}</row>
<row input="-0.3576" format="0.##" expected="-0.36">{fn:format-number(-0.3576, "0.##")}</row>
<row input="-0.3576" format="0.###" expected="-0.358">{fn:format-number(-0.3576, "0.###")}</row>
<row input="-0.3576" format="0.####" expected="-0.3576">{fn:format-number(-0.3576, "0.####")}</row>
<row input="-0.3576" format="0.00" expected="-0.36">{fn:format-number(-0.3576, "0.00")}</row>
<row input="0.000" format="0.##" expected="0.00">{fn:format-number(0.000, "0.##")}</row>
<row input="0" format="0.00" expected="0.00">{fn:format-number(0, "0.00")}</row>
<row input="0.3" format="0.####" expected="0.3">{fn:format-number(0.3, "0.####")}</row>
<row input="0.3" format="0.0000" expected="0.3000">{fn:format-number(0.3, "0.0000")}</row>
<row input="17" format="0.##" expected="17">{fn:format-number(17, "0.##")}</row>
<row input="17" format="0.00" expected="17.00">{fn:format-number(17, "0.00")}</row>
<row input="17.4555" format="0.##" expected="17.46">{fn:format-number(17.4555, "0.##")}</row>
<row input="-17.4555" format="0.##" expected="-17.46">{fn:format-number(-17.4555, "0.##")}</row>
</rows>return $result
The result of this is as follows:
<rows>
<row input="0.3576" format="0.##" expected="0.36">.36</row>
<row input="-0.3576" format="0.##" expected="-0.36">-.36</row>
<row input="-0.3576" format="0.###" expected="-0.358">-.358</row>
<row input="-0.3576" format="0.####" expected="-0.3576">-.3576</row>
<row input="-0.3576" format="0.00" expected="-0.36">-.36</row>
<row input="0.000" format="0.##" expected="0.00">0</row>
<row input="0" format="0.00" expected="0.00">0.00</row>
<row input="0.3" format="0.####" expected="0.3">.3</row>
<row input="0.3" format="0.0000" expected="0.3000">.3000</row>
<row input="17" format="0.##" expected="17">17</row>
<row input="17" format="0.00" expected="17.00">17.00</row>
<row input="17.4555" format="0.##" expected="17.46">17.46</row>
<row input="-17.4555" format="0.##" expected="-17.46">-17.46</row>
</rows>Unfortunately two things seem to go wrong here:
· Zero in the picture string is ignored for negative numbers.
· The zero before decimal is ignored for amounts between zero and one (but does occur for 0.0000 for example).
I hope someone can take a look at this and provide a fix.
I tried messing around with string replace functions where I would just replace -. with -0. and prefix the result for a decimal point with a zero, but this really isn't present and requires a workaround for something that is very common in my code.
Using the latest basex (8.4.4) on Windows Server 2012 under Java 8 (64 bit, last update just now: update 91).
Thanks in advance and best regards,
Michaël Stoop
CG
Christian Grün3 Jun 2016 09:41 #
Hi Michael,
Thanks for your mail. You are right, the
fn:format-number function is still work in progress. There has been a lot discussion on numerous details of the function in the working group (see e.g. [1]), and we now need to catch up. I think I’ll give you an update once the changes have been incorporated.
Cheers,
Christian
PS: fn:format-number(0.000, "0.##") should yield "0" instead of "0.00".
[1] https://www.w3.org/Bugs/Public/show_bug.cgi?id=28845Quoted text · Michael Stoop · 138 lines
On Fri, Jun 3, 2016 at 9:26 AM, Michael Stoop <m.stoop at calvi.nl> wrote: > Good morning, > > > > First of all thank you for your great work on basex. I’ve been phasing out > Zorba in favour of BaseX for excellent large file support and all around > better documentation and support. > > > > However, I’m running into some issues in formatting numbers in basex 8.4.4 > using fn:format-number (using > http://www.w3schools.com/xsl/func_formatnumber.asp) > > > > To describe this behavior I wrote a simple xquery: > > > > let $result := > > <rows> > > <row input="0.3576" format="0.##" > expected="0.36">{fn:format-number(0.3576, "0.##")}</row> > > <row input="-0.3576" format="0.##" > expected="-0.36">{fn:format-number(-0.3576, "0.##")}</row> > > <row input="-0.3576" format="0.###" > expected="-0.358">{fn:format-number(-0.3576, "0.###")}</row> > > <row input="-0.3576" format="0.####" > expected="-0.3576">{fn:format-number(-0.3576, "0.####")}</row> > > <row input="-0.3576" format="0.00" > expected="-0.36">{fn:format-number(-0.3576, "0.00")}</row> > > <row input="0.000" format="0.##" expected="0.00">{fn:format-number(0.000, > "0.##")}</row> > > <row input="0" format="0.00" expected="0.00">{fn:format-number(0, > "0.00")}</row> > > <row input="0.3" format="0.####" expected="0.3">{fn:format-number(0.3, > "0.####")}</row> > > <row input="0.3" format="0.0000" expected="0.3000">{fn:format-number(0.3, > "0.0000")}</row> > > <row input="17" format="0.##" expected="17">{fn:format-number(17, > "0.##")}</row> > > <row input="17" format="0.00" expected="17.00">{fn:format-number(17, > "0.00")}</row> > > <row input="17.4555" format="0.##" > expected="17.46">{fn:format-number(17.4555, "0.##")}</row> > > <row input="-17.4555" format="0.##" > expected="-17.46">{fn:format-number(-17.4555, "0.##")}</row> > > </rows> > > > > return $result > > > > The result of this is as follows: > > <rows> > > <row input="0.3576" format="0.##" expected="0.36">.36</row> > > <row input="-0.3576" format="0.##" expected="-0.36">-.36</row> > > <row input="-0.3576" format="0.###" expected="-0.358">-.358</row> > > <row input="-0.3576" format="0.####" expected="-0.3576">-.3576</row> > > <row input="-0.3576" format="0.00" expected="-0.36">-.36</row> > > <row input="0.000" format="0.##" expected="0.00">0</row> > > <row input="0" format="0.00" expected="0.00">0.00</row> > > <row input="0.3" format="0.####" expected="0.3">.3</row> > > <row input="0.3" format="0.0000" expected="0.3000">.3000</row> > > <row input="17" format="0.##" expected="17">17</row> > > <row input="17" format="0.00" expected="17.00">17.00</row> > > <row input="17.4555" format="0.##" expected="17.46">17.46</row> > > <row input="-17.4555" format="0.##" expected="-17.46">-17.46</row> > > </rows> > > > > > > Unfortunately two things seem to go wrong here: > > · Zero in the picture string is ignored for negative numbers. > > · The zero before decimal is ignored for amounts between zero and > one (but does occur for 0.0000 for example). > > > > I hope someone can take a look at this and provide a fix. > > I tried messing around with string replace functions where I would just > replace -. with -0. and prefix the result for a decimal point with a zero, > but this really isn’t present and requires a workaround for something that > is very common in my code. > > > > Using the latest basex (8.4.4) on Windows Server 2012 under Java 8 (64 bit, > last update just now: update 91). > > > > Thanks in advance and best regards, > > Michaël Stoop > > > >
CG
Hi Michael,
I spent some free minutes with the
fn:format-number function. With the latest stable snapshot, the leading zero should now be included in your result [1]. Please note that the function implementation is still subject to change, in particular regarding the new exponential notation.
Hope this helps,
Christian
[1] http://files.basex.org/releases/latest/Quoted text · Michael Stoop · 138 lines
On Fri, Jun 3, 2016 at 9:26 AM, Michael Stoop <m.stoop at calvi.nl> wrote: > Good morning, > > > > First of all thank you for your great work on basex. I’ve been phasing out > Zorba in favour of BaseX for excellent large file support and all around > better documentation and support. > > > > However, I’m running into some issues in formatting numbers in basex 8.4.4 > using fn:format-number (using > http://www.w3schools.com/xsl/func_formatnumber.asp) > > > > To describe this behavior I wrote a simple xquery: > > > > let $result := > > <rows> > > <row input="0.3576" format="0.##" > expected="0.36">{fn:format-number(0.3576, "0.##")}</row> > > <row input="-0.3576" format="0.##" > expected="-0.36">{fn:format-number(-0.3576, "0.##")}</row> > > <row input="-0.3576" format="0.###" > expected="-0.358">{fn:format-number(-0.3576, "0.###")}</row> > > <row input="-0.3576" format="0.####" > expected="-0.3576">{fn:format-number(-0.3576, "0.####")}</row> > > <row input="-0.3576" format="0.00" > expected="-0.36">{fn:format-number(-0.3576, "0.00")}</row> > > <row input="0.000" format="0.##" expected="0.00">{fn:format-number(0.000, > "0.##")}</row> > > <row input="0" format="0.00" expected="0.00">{fn:format-number(0, > "0.00")}</row> > > <row input="0.3" format="0.####" expected="0.3">{fn:format-number(0.3, > "0.####")}</row> > > <row input="0.3" format="0.0000" expected="0.3000">{fn:format-number(0.3, > "0.0000")}</row> > > <row input="17" format="0.##" expected="17">{fn:format-number(17, > "0.##")}</row> > > <row input="17" format="0.00" expected="17.00">{fn:format-number(17, > "0.00")}</row> > > <row input="17.4555" format="0.##" > expected="17.46">{fn:format-number(17.4555, "0.##")}</row> > > <row input="-17.4555" format="0.##" > expected="-17.46">{fn:format-number(-17.4555, "0.##")}</row> > > </rows> > > > > return $result > > > > The result of this is as follows: > > <rows> > > <row input="0.3576" format="0.##" expected="0.36">.36</row> > > <row input="-0.3576" format="0.##" expected="-0.36">-.36</row> > > <row input="-0.3576" format="0.###" expected="-0.358">-.358</row> > > <row input="-0.3576" format="0.####" expected="-0.3576">-.3576</row> > > <row input="-0.3576" format="0.00" expected="-0.36">-.36</row> > > <row input="0.000" format="0.##" expected="0.00">0</row> > > <row input="0" format="0.00" expected="0.00">0.00</row> > > <row input="0.3" format="0.####" expected="0.3">.3</row> > > <row input="0.3" format="0.0000" expected="0.3000">.3000</row> > > <row input="17" format="0.##" expected="17">17</row> > > <row input="17" format="0.00" expected="17.00">17.00</row> > > <row input="17.4555" format="0.##" expected="17.46">17.46</row> > > <row input="-17.4555" format="0.##" expected="-17.46">-17.46</row> > > </rows> > > > > > > Unfortunately two things seem to go wrong here: > > · Zero in the picture string is ignored for negative numbers. > > · The zero before decimal is ignored for amounts between zero and > one (but does occur for 0.0000 for example). > > > > I hope someone can take a look at this and provide a fix. > > I tried messing around with string replace functions where I would just > replace -. with -0. and prefix the result for a decimal point with a zero, > but this really isn’t present and requires a workaround for something that > is very common in my code. > > > > Using the latest basex (8.4.4) on Windows Server 2012 under Java 8 (64 bit, > last update just now: update 91). > > > > Thanks in advance and best regards, > > Michaël Stoop > > > >
Written for BaseX 8.4. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery