| Convert-Number-Greek documentation | Contained in the Convert-Number-Greek distribution. |
Convert::Number::Greek - Convert between Arabic and Greek numerals
Version 0.02
use Convert::Number::Greek qw'num2greek greek2num';
$greek_number = num2greek 1996;
# $greek_number now contains
# "\x{375}\x{3b1}\x{3e1}\x{3df}\x{3db}\x{374}"
$number = greek2num "\x{3b5}\x{3c3}\x{3c4}\x{3b6}'";
# $number now contains 567
This module provides subroutines for converting between Arabic and Greek numbers.
num2greek converts an Arabic numeral to a Greek numeral in the form of a Unicode string the syntax is as follows:
NUMBER is the number to convert. It should be a string of digits, nothing more (see BUGS, below). OPTIONS (optional) is a reference to a hash of boolean options. The options available are as follows:
Option Name Default Value Description
upper 0 Use uppercase Greek letters
uc 0 " " " "
stigma 1 Use the stigma for 6 as opposed to
sigma followed by tau
arch_koppa 0 Use the archaic koppa instead of
the modern one
numbersign 1 Append a Greek number sign (U+0374)
to the resulting string
When you specify options, undef is treated as false, so
num2greek $some_number, { uc => 1, stigma }
actually means
num2greek $some_number, { uc => 1, stigma => 0 }
The greek2num function parses a Greek numbers and returns the
Arabic equivalent.
STRING is a string consisting of a Greek number. Anything following
the number will be ignored, but will raise a warning if
use warnings 'numeric' is on (unless it's just whitespace).
Currently no options are available.
None by default, but you get num2greek and greek2num if you ask
for them (politely).
The greek2num function will trigger a "non-numeric" warning if you
use warnings 'numeric'.
This module requires perl 5.8.0 or later, though the earliest version I have tested it with is 5.8.1.
The num2greek function does not yet have any error-checking
mechanism in place. The input should be a string of Arabic digits, or
at least a value that stringifies to such. Using an argument that does
not fit this description may produce nonsensical results.
Father Chrysostomos <sprout @cpan.org>
| Convert-Number-Greek documentation | Contained in the Convert-Number-Greek distribution. |
package Convert::Number::Greek; require 5.008; use strict; # :-( use warnings; # :-( use POSIX 'floor'; use utf8; require Exporter;
use vars qw[ @ISA @EXPORT_OK $VERSION @greek_digits @greek_digits_uc ]; $VERSION = '0.02'; @ISA = 'Exporter'; @EXPORT_OK = qw'num2greek greek2num';
@greek_digits = ( ['', qw[α β γ δ ε Ï Î¶ η θ]], ['', qw[ι κ λ μ ν ξ ο Ï Ï]], ['', qw[Ï Ï Ï Ï Ï Ï Ï Ï Ï¡]], ); @greek_digits_uc = ( ['', qw[Î Î Î Î Î Ï Î Î Î]], ['', qw[Î Î Î Î Î Î Î Î Ï]], ['', qw[Ρ Σ Τ Î¥ Φ Χ Ψ Ω Ï ]], ); sub num2greek ($;$) { my ($n, $options) = @_; my $ret; $options ||= {}; my @digits = $$options{uc} || $$options{upper} ? @greek_digits_uc : @greek_digits; exists $$options{stigma} and !$$options{stigma} and local $digits[0][6] = $digits[0][6] eq 'Ï' ? 'ÏÏ' : 'ΣΤ'; $$options{arch_koppa} and local $digits[1][9] = $digits[1][9] eq 'Ï' ? 'Ï' : 'Ï'; for my $place ( reverse 0 .. length($n) - 1 ) { my $digit = substr $n, length($n) - $place - 1, 1; $ret .= '͵' x floor($place / 3) . # thousands indicator $digits[$place % 3][$digit]; } $ret .= 'Í´' unless exists $$options{numbersign} and !$$options{numbersign}; $ret; }
our %greek_digit_2_num = qw( α 1 β 2 γ 3 δ 4 ε 5 Ï 6 ζ 7 η 8 θ 9 ι 10 κ 20 λ 30 μ 40 ν 50 ξ 60 ο 70 Ï 80 Ï 90 Ï 100 Ï 200 Ï 300 Ï 400 Ï 500 Ï 600 Ï 700 Ï 800 Ï¡ 900 Î 1 Î 2 Î 3 Î 4 Î 5 Ï 6 Î 7 Î 8 Î 9 Î 10 Î 20 Î 30 Î 40 Î 50 Î 60 Î 70 Î 80 Ï 90 Ρ 100 Σ 200 Τ 300 Î¥ 400 Φ 500 Χ 600 Ψ 700 Ω 800 Ï 900 Ï 90 Ï 90 á¾³ 1000 á¿ 8000 ῳ 800000 á¾¼ 1000 á¿ 8000 ῼ 800000 ); sub greek2num ($;$) { my($n,$ret,$thousands,$digit) = $_[0]; $n =~ s/^\s+//; while (length $n) { $thousands = $n =~ s/^([͵,]+)// && length $1; if($n =~ s/^ÏÏ//i) { $digit = 6; } elsif(exists $greek_digit_2_num{substr $n,0,1}) { $digit = $greek_digit_2_num{substr $n,0,1,''}; } else { $n =~ s/^['âʹ´Î]?\s*//; # straight quote, smart length $n or last; # quote, number sign, warnings::warnif( # oxia, tonos numeric => qq/Argument "$_[0]" isn't numeric in greek2num/ ); last; } $ret += $digit * 1000**$thousands; } $ret; }