xsl-list
[Top] [All Lists]

Re: [xsl] Function that reduces the number of digits to the right of the decimal point?

2020-06-23 11:26:18
OK, here is a correct one:

for $pPrec in max((0, $pPrecision))
  return
      floor($pNum*math:exp10($pPrec))    div     math:exp10($pPrec)

On Tue, Jun 23, 2020 at 9:12 AM Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com>
wrote:

Something like:

floor($pNum*math:exp10($pPrecision)) div math:exp10($pPrecision)


Untested !!!!

On Tue, Jun 23, 2020 at 7:28 AM Roger L Costello 
costello(_at_)mitre(_dot_)org <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi Folks,

$num is a variable holding a decimal value.

$desired-number-of-digits-after-decimal-point is a variable holding an
integer value.

I want a function that reduces the number of digits to the right of the
decimal point in $num according to the value specified in
$desired-number-of-digits-after-decimal-point

Example #1:
$num = 42.366978
$desired-number-of-digits-after-decimal-point = 2
The result of applying the function is: 42.36

Example #2:
$num = 42.366978
$desired-number-of-digits-after-decimal-point = 0
The result of applying the function is: 42

Below is one way to implement the function. Is there a better way? By
"better" I mean simpler, more correct.

Also, is there a better name for the function?  By "better" I mean
shorter, more standardized. /Roger
-----------------------------------------------------
<xsl:function name="f:reduce-number-of-digits-after-decimal-point"
as="xs:string">
    <xsl:param name="num" as="xs:string" />
    <xsl:param name="desired-number-of-digits-after-decimal-point"
as="xs:integer" />

    <xsl:variable name="whole-part" select="substring-before($num, '.')"/>
    <xsl:variable name="fraction-part" select="substring-after($num,
'.')"/>
    <xsl:variable name="reduced-fraction-part"
select="substring($fraction-part, 1,
$desired-number-of-digits-after-decimal-point)"/>
    <xsl:choose>
        <xsl:when test="$desired-number-of-digits-after-decimal-point gt
0">
            <xsl:value-of select="concat($whole-part, '.',
$reduced-fraction-part)"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$whole-part"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:function>




--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>