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

Subject: Re: [xsl] Function that reduces the number of digits to the rightof the decimal point?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Jun 2020 16:31:14 -0000
Sorry, should be (4.99 * 10 idiv 1) div 10

Michael Kay

floor($x) does the same as $x idiv 1 for positive numbers, it's different for
negative.


> On 23 Jun 2020, at 17:25, Roger L Costello costello@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Michael Kay wrote:
>
> (4.99 * 10 idiv 10) gives 4.9
>
> But, but, but, .
>
> 4.99 * 10 = 49.9
> 49.9 idiv 10 = 4
>
> Right?
>
> /Roger
>
> From: Michael Kay mike@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Tuesday, June 23, 2020 11:48 AM
> To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Subject: [EXT] Re: [xsl] Function that reduces the number of digits to the
rightof the decimal point?
>
> Do you absolutely want truncation (so 4.99 becomes 4.9) or will rounding do
(so 4.99 becomes 5.0)?
>
> If rounding will do, then use round($x, -$desired-digits).
>
> If you really want truncation then consider the "idiv" operator, for example
(4.99 * 10 idiv 10) gives 4.9: you can construct the value 10 as math:pow(10,
$desired-digits).
>
> Michael Kay
> Saxonica
>
>
> On 23 Jun 2020, at 15:28, Roger L Costello mailto:costello@xxxxxxxxx
<mailto: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>
>
> http://www.mulberrytech.com/xsl/xsl-list
> http://lists.mulberrytech.com/unsub/xsl-list/673357 ()

Current Thread