| 1 | # For Emacs: -*- mode:cperl; mode:folding coding:iso-8859-1 -*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Started by Vitor Serra Mori at 2004-05-19 | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # PPCG: 0.7 | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Lingua::SV::Num2Word; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 15 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 16 | 1 | @EXPORT_OK = qw(&num2sv_cardinal); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # {{{ num2sv_cardinal convert number to text | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | 36 | ▶ | sub num2sv_cardinal { | ||||||||||||||||||||||||||
|
Subroutine
num2sv_cardinal: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 36 | ▶ | my $positive = ($_[0]>=0) ? shift : return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 36 | my $out; | |||||||||||||||||||||||||||
| 24 | 36 | my @tokens1 = qw(noll ett två tre fyra fem sex sju åtta nio tio elva | |||||||||||||||||||||||||||
| 25 | tolv tretton fjorton femton sexton sjutton arton nitton); # 0-19 Cardinals | ||||||||||||||||||||||||||||
| 26 | 36 | my @tokens2 = qw(tjugo trettio fyrtio femtio sextio sjutio åttio nittio); # 20-90 Cardinals (end with zero) | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 36 | ▶ | return $tokens1[$positive] if($positive < 20); # interval 0 - 19 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 30 | ▶ | if($positive < 100) { # interval 20 - 99 | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 31 | 3 | my @num = split '',$positive; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | $out = $tokens2[$num[0]-2]; | |||||||||||||||||||||||||||
| 34 | 3 | ▶ | $out .= $tokens1[$num[1]] if ($num[1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } elsif($positive < 1000) { # interval 100 - 999 | ||||||||||||||||||||||||||||
| 36 | 14 | my @num = split '',$positive; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 14 | $out = $tokens1[$num[0]].'hundra'; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 14 | ▶ | if ((int $num[1].$num[2]) < 20 && (int $num[1].$num[2])>0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
int $num[1] . $num[2] < 20 and int $num[1] . $num[2] > 0
MC/DC100%2/2
int $num[1] . $num[2] < 20 and int $num[1] . $num[2] > 0
int $num[1] . $num[2] < 20
int $num[1] . $num[2] > 0
Truth table100%3/3
A and B
A= int $num[1] . $num[2] < 20B= int $num[1] . $num[2] > 0
| |||||||||||||||||||||||||||||
| 41 | 1 | $out .= &num2sv_cardinal(int $num[1].$num[2]); | |||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 13 | ▶ | $out .= $tokens2[$num[1]-2] if($num[1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 13 | ▶ | $out .= $tokens1[$num[2]] if($num[2]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } elsif($positive < 1000_000) { # interval 1000 - 999_999 | ||||||||||||||||||||||||||||
| 47 | 9 | my @num = split '',$positive; | |||||||||||||||||||||||||||
| 48 | 9 | my @sub = splice @num,-3; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 9 | $out = &num2sv_cardinal(int join '',@num); | |||||||||||||||||||||||||||
| 51 | 9 | $out .= 'tusen'; | |||||||||||||||||||||||||||
| 52 | 9 | ▶ | $out .= &num2sv_cardinal(int join '',@sub) if (int(join "",@sub) >0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } elsif($positive < 1_000_000_000) { # interval 1_000_000 - 999_999_999 | ||||||||||||||||||||||||||||
| 54 | 3 | my @num = split '',$positive; | |||||||||||||||||||||||||||
| 55 | 3 | my @sub = splice @num,-6; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | $out = &num2sv_cardinal(int join '',@num); | |||||||||||||||||||||||||||
| 58 | 3 | $out .= ' miljoner '; | |||||||||||||||||||||||||||
| 59 | 3 | ▶ | $out .= &num2sv_cardinal(int join '',@sub) if (int(join "",@sub) >0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 30 | return $out; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # }}} | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | 1; | |||||||||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||||||||||