xsl-list
[Top] [All Lists]

Re: [xsl] Truncate input

2006-09-11 07:56:38
Hi M Core,

Not sure if your question is about XSLT or XSL, since there's no mention of either in your post.

If you want to format a number, you can use <xsl:decimal-format /> with format-number(value, format, name). The name in format-number refers to a name you predefined with <xsl:decimal-format />. Recall that <xsl:decimal-number /> is a top-level element. This means it must be right under your <xsl:stylesheet> instruction.

You may also choose to format your number using the <xsl:number /> instruction or using string concatenation functions.

If you need rounding, you can use the round(number) xpath function, but since you say that 3,45678 must become 3,45, I guess you need truncating. If you use round(), be aware that it only returns integers.

-- using <xsl:number />
<xsl:number value="$yourinput" format="1.00" />

-- using round()
<xsl:value-of select="round($yourinput * 100) div 100" />

-- using fixed() (will truncate)
<xsl:value-of select="fixed($yourinput * 100) div 100" />

-- using string functions:
<xsl:value-of select="concat ( substring-before( $yourinput, '.' ) , '.' , substring ( substring-after ( $yourinput, '.' ) , 1 , 2 ) )" />

You may have noticed that all these functions take input as a number that has a dot for decimal. You use a comma for decimal, so you may need to convert it. The easy thing to do that is using translate(string, tr, repl), I think:

-- converting your number:
<xsl:value-of select="translate ( $yournumber , ',' , '.' )" />

All the above snippets work with XSLT 1.0. But you can use them with 2.0 if you like or need it. There are many more possibilities.

Cheers,
Abel Braaksma
http://abelleba.metacarpus.com


m(_dot_)core(_at_)aimconsulting(_dot_)it wrote:
Hi all,

i need to truncate input that can be write in a box i have a var v1=2 if after 
coma there is more then v1 numbers then it must be truncate to v1 es...

if i insert 3,45678 in the input it let me write only 3,45 because v1=2
i think this must be done in the tags <input > </input>
i'm already in the case that the number is real ecc..

How can i do?..is there an option like <input format="">?

thx for replies

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>