NAME
Lingua::EN::Fractions - convert "3/4" into "three quarters", etc
SYNOPSIS
use Lingua::EN::Fractions qw/ fraction2words /;
my $fraction = '3/4';
my $as_words = fraction2words($fraction);
Or using Number::Fraction:
use Number::Fraction;
my $fraction = Number::Fraction->new(2, 7);
my $as_words = fraction2words($fraction);
DESCRIPTION
This module provides a function, fraction2words
, which takes a string containing a fraction and returns the English phrase for that fraction. If no fraction was found in the input, then undef
is returned.
For example
fraction2words('1/2'); # "one half"
fraction2words('3/4'); # "three quarters"
fraction2words('5/17'); # "five seventeenths"
fraction2words('5'); # undef
fraction2words('-3/5'); # "minus three fifths"
You can also pass a whole number ahead of the fraction:
fraction2words('1 1/2'); # "one and a half"
fraction2words('-1 1/8'); # "minus one and an eighth"
fraction2words('12 3/4'); # "twelve and three quarters"
Note that instead of "one and one half", you'll get back "one and a half".
Unicode fraction characters
As of version 0.05, certain Unicode characters are also supported. For example:
fraction2words('½') # "one half"
fraction2words('1⅜') # "one and three eighths"
fraction2words('-1⅘') # "minus one and four fifths"
You can also use the Unicode FRACTION SLASH, which is a different character from the regular slash:
fraction2words('1/2') # "one half"
fraction2words('1⁄2') # "one half"
As of version 0.06, you an also use the Unicode MINUS SIGN:
fraction2words('−1/2') # "minus one half"
fraction2words('−⅘') # "minus four fifths"
At the moment, the DIVISION SLASH character isn't handled. Feel free to tell me if you think I got that wrong.
Working with Number::Fraction
You can also pass in a fraction represented using Number::Fraction:
$fraction = Number::Fraction->new(2, 7);
$as_words = fraction2words($fraction); # "two sevenths"
CAVEATS
At the moment, no attempt is made to simplify the fraction, so '5/2'
will return "five halves" rather than "two and a half". Note though, that if you're using Number::Fraction, then it does normalise fractions, so "3/6" will become "1/2".
At the moment it's not very robust to weird inputs.
SEE ALSO
Lingua::EN::Numbers, Lingua::EN::Numbers::Ordinate, Lingua::EN::Numbers::Years - other modules for converting numbers into words.
Number::Fraction - a class for representing fractions and operations on them.
REPOSITORY
https://github.com/neilb/Lingua-EN-Fractions
AUTHOR
Neil Bowers <neilb@cpan.org>
This module was suggested by Sean Burke, who created the other Lingua::EN::*
modules that I now maintain.
COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.