16 Aug 2017 18:20:53 UTC
- Distribution: Lingua-EN-Fractions
- Module version: 0.09
- Source (raw)
- Browse (raw)
- How to Contribute
- Repository (git clone)
- Testers (557 / 0 / 0)
- KwaliteeBus factor: 1
- 98.46% Coverage
- License: perl_5
- Perl: v5.8.0
- Activity24 month
- Download (11.88KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version
- SEE ALSO
- COPYRIGHT AND LICENSE
Lingua::EN::Fractions - convert "3/4" into "three quarters", etc
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);
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
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".
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.
You can also pass in a fraction represented using Number::Fraction:
$fraction = Number::Fraction->new(2, 7); $as_words = fraction2words($fraction); # "two sevenths"
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.
Number::Fraction - a class for representing fractions and operations on them.
Neil Bowers <firstname.lastname@example.org>
This module was suggested by Sean Burke, who created the other
Lingua::EN::*modules that I now maintain.
This software is copyright (c) 2014 by Neil Bowers <email@example.com>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Module Install Instructions
To install Lingua::EN::Fractions, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Lingua::EN::Fractions
For more information on module installation, please visit the detailed CPAN module installation guide.