0

I have a requirement where i should get absolute value for -0.0000000005 .I have tried with <xsl:value-of select='abs(-0.0000000005)' /> and <xsl:value-of select="translate(-0.0000000005, '-', '')" /> .But nothing is working and getting always alpha numeric value as 5.0E10.Please help here to get correct absolute value form any decimal number.

need code for correct absolute value to get from decimal numbers

  • Can you tell us which XSLT processor that you are using and which version of XSLT? In saxonica XSLT3, I'm getting precisely ```0.0000000005``` from the instruction ``````. – al.truisme Dec 06 '22 at 07:37
  • Hi Truisme,,It is XSLT version 1.0 – Sandhya Balireddy Dec 06 '22 at 08:11
  • I'm afraid that I haven't found a simple solution for XSLT 1.0. For XSLT 2.0 and above, the answer that I've provided below should work for you. – al.truisme Dec 06 '22 at 10:00
  • @Sandhya, which XSLT processor are you using? And when you say "nothing is working", please tell us how it fails. I'm surprised any XSLT 1.0 processor would produce output using exponential notation: we need more information to help you with this. – Michael Kay Dec 06 '22 at 11:21
  • @MichaelKay The `libxslt` processor will produce scientific notation if the value is already a number (as it is in the given example). – michael.hor257k Dec 06 '22 at 11:54
  • @SandhyaBalireddy Perhaps this might help: https://stackoverflow.com/a/62902878/3016153. If not, please provide a way to reproduce the problem from input - see [mcve]. Also please identify the processor - see: https://stackoverflow.com/a/25245033/3016153. It's not possible you used `abs()` with an XSLT 1.0 processor without getting an error. – michael.hor257k Dec 06 '22 at 11:59
  • @michael.hor257k Thanks, noted. This isn't conformant XSLT 1.0 behaviour, IIRC. – Michael Kay Dec 06 '22 at 14:12

2 Answers2

0

Depending upon the XSLT processor, this might work for you:

<xsl:value-of select="xs:decimal(abs(-0.0000000005))" />
al.truisme
  • 450
  • 2
  • 11
0

You could try using the format-number function to explicitly specify how the number is formatted. For example something like this:

<xsl:value-of select="format-number(abs(-0.0000000005), '0.##########')" />

(Note that the abs function is not part of XPath 1.0/XSLT 1.0. Users of XSLT 1.0 may want to check if their XSLT processor supports the math:abs extension function from EXSLT.)

Jukka Matilainen
  • 9,608
  • 1
  • 25
  • 19