Search

This template converts an integer into a human-readable string.

Simple example

Pass the number as a string parameter.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
</xsl:call-template>

Result: one hundred and twenty-three

Negative numbers

Numbers can also be negative.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'-8'" />
</xsl:call-template>

Result: minus eight

Minimum values

If the number is less than a specified threshold, only the threshold is shown.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
    <xsl:with-param name="minimum" select="'150'" />
</xsl:call-template>

Result: less than one hundred and fifty

Maximum values

If the number is greater than a specified threshold, only the threshold is shown.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
    <xsl:with-param name="maximum" select="'100'" />
</xsl:call-template>

Result: more than one hundred

Minimum and maximum

Maximum and minimum can be used at the same time. They have no effect when the number is within the specified bounds.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
    <xsl:with-param name="minimum" select="'100'" />
    <xsl:with-param name="maximum" select="'150'" />
</xsl:call-template>

Result: one hundred and twenty-three (number is within bounds)

Rounding resolution

The number can be rounded to the nearest n. Resolution can be any number, but I suggest using 5, 10, 100, 500, 1000 etc, depending on context.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
    <xsl:with-param name="resolution" select="'10'"/>
</xsl:call-template>

Result: about one hundred and twenty

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
    <xsl:with-param name="resolution" select="'100'"/>
</xsl:call-template>

Result: about one hundred

Auto-resolution

When original number size can vary greatly using a resolution value of -1 will auto-calculate the appropriate rounding resolution based on predefined ranges.

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123'" />
    <xsl:with-param name="resolution" select="'-1'"/>
</xsl:call-template>

Result: about one hundred and twenty

<xsl:call-template name="number-to-words">
    <xsl:with-param name="number" select="'123456'" />
    <xsl:with-param name="resolution" select="'-1'"/>
</xsl:call-template>

Result: about one hundred and twenty-five thousand

Download

http://symphony21.com/downloads/xslt/file/20486/

Nick -

This is very helpful! Thanks for posting!

Brilliant nick! Just what I needed for my website :)

one thing about the semantics: whenever you use the term "and" in a number, that "and" usually refers to a decimal point. so rather than one hundred and twenty equating to 120, it really is referring to 100.20.

Ha, Rowan that's either you being incredibly sarcastic or a case of good timing ;-)

Dale, not sure I follow about the "and" as referring to the decimal point. I know in the US it would be said "one hundred twenty three", but in the UK (and elsewhere?) the "and" is the common usage. I've not heard of "and" referring to the decimal point. Could you maybe show me an example? Perhaps my template can accommodate several variants.

The number "1,234.5" should be written as "one thousand, two hundred and thirty-four point five" which is proper grammatical form for the US; I imagine it would be for the UK as well.

The only time "and" refers to a decimal point is if you are saying "1.2" as "one and two-tenths".

You can also combine the two examples and write "1,234.5" as "one thousand, two hundred and thirty-four and one-half", which would be acceptable though not always recommended.

...What? I'm a grammar nazi.

@doug - never heard it that way before, but makes sense to a certain extent now that you mention it. "1.2" would never be said as "one and two."
@nick - regardless of what doug or i said, the point is, whether it's and or point, it should only be where the decimal falls.

it makes sense to use point when you are strictly talking about numbers, but what about money? you wouldn't really say "one thousand two hundred thirty-four point five," but would substitute and for point in this case. so what it comes down to for this utility is context. from the looks of it, this seems like it's just strictly for numbers, and nothing to do with currency, correct?

Correct — I intended this to work only with whole numbers (integers). We've found the primary use for this to be when there are a large number of views, votes or comments on a site and we want to summarise the number quickly. The "-1" resolution means that if one "object" (what users are viewing, voting or commenting on) becomes vastly more popular than others, the rounding is still taken care of.

Perhaps we're falling foul of geographical differences. I'm with Doug.

1,234.5 = one thousand, two hundred and thirty four point five

From the US TV I've watched, it's Americans who drop the "and" within the full number itself, dropping it to "one thousand, two hundred thirty four". Which makes me shudder every time I hear it ;-)

For numbers, or time, you'd need a different routine entirely. The number before the point would need to have a "pounds" suffix, and the point would need to say "and" followed by "pence". Additionally the number after the point would need to be set to tenths.

1,234.5 would need to resolve to £1,234.50, which would resolve to: "One thousand, two hundred and thirty four pounds, and fifty pence". Two "ands".

In Europe, anyway.

yes, that seems to be a geographical difference...although many people here in the states don't speak no good english =).

but AFAIK, nick, $1,234.50 would be pronounced in a way that would make you shudder =)

"One thousand two hundred and thirty-four dollars and fifty cents" for us Aussies, which should be mighty similar if not identical our Brit brethren.

Is it "one shizzle two nizzle and thirdy-foor dizzle and fiddy cents, dawg" in the states?

not in the states...it's more like: "give me all your money before i shank you!", i think =)

Hey! Play nice! We gave you people jQuery, Jeffrey Zeldman and the W3C. Don't forget that. :P

And we gave you the Web ;-)

We have some help from the English and the boys at CERN.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details