[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Function that reduces the number of digits to the

Subject: Re: Function that reduces the number of digits to the right of the decimal point?
From: "David Carlisle d.p.carlisle@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Jun 2020 14:49:42 -0000
Re:  Function that reduces the number of digits to the
https://www.w3.org/TR/xpath-functions/#func-round

?

David


On Tue, 23 Jun 2020 at 15:28, Roger L Costello costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 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>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.