| 1 | # -*- mode: perl; -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Math::BigInt::Named::English; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Math::BigInt::Named; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | our @ISA = qw< Math::BigInt::Named >; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | my $SMALL = [ qw/ | |||||||||||||||||||||||||||||||||||||||||
| 14 | zero | ||||||||||||||||||||||||||||||||||||||||||
| 15 | one | ||||||||||||||||||||||||||||||||||||||||||
| 16 | two | ||||||||||||||||||||||||||||||||||||||||||
| 17 | three | ||||||||||||||||||||||||||||||||||||||||||
| 18 | four | ||||||||||||||||||||||||||||||||||||||||||
| 19 | five | ||||||||||||||||||||||||||||||||||||||||||
| 20 | six | ||||||||||||||||||||||||||||||||||||||||||
| 21 | seven | ||||||||||||||||||||||||||||||||||||||||||
| 22 | eight | ||||||||||||||||||||||||||||||||||||||||||
| 23 | nine | ||||||||||||||||||||||||||||||||||||||||||
| 24 | ten | ||||||||||||||||||||||||||||||||||||||||||
| 25 | eleven | ||||||||||||||||||||||||||||||||||||||||||
| 26 | twelve | ||||||||||||||||||||||||||||||||||||||||||
| 27 | thirteen | ||||||||||||||||||||||||||||||||||||||||||
| 28 | fourteen | ||||||||||||||||||||||||||||||||||||||||||
| 29 | fifteen | ||||||||||||||||||||||||||||||||||||||||||
| 30 | sixteen | ||||||||||||||||||||||||||||||||||||||||||
| 31 | seventeen | ||||||||||||||||||||||||||||||||||||||||||
| 32 | eighteen | ||||||||||||||||||||||||||||||||||||||||||
| 33 | nineteen | ||||||||||||||||||||||||||||||||||||||||||
| 34 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | my $TENS = [ qw / | |||||||||||||||||||||||||||||||||||||||||
| 37 | ten | ||||||||||||||||||||||||||||||||||||||||||
| 38 | twenty | ||||||||||||||||||||||||||||||||||||||||||
| 39 | thirty | ||||||||||||||||||||||||||||||||||||||||||
| 40 | fourty | ||||||||||||||||||||||||||||||||||||||||||
| 41 | fifty | ||||||||||||||||||||||||||||||||||||||||||
| 42 | sixty | ||||||||||||||||||||||||||||||||||||||||||
| 43 | seventy | ||||||||||||||||||||||||||||||||||||||||||
| 44 | eighty | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ninety | ||||||||||||||||||||||||||||||||||||||||||
| 46 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | my $HUNDREDS = [ qw / | |||||||||||||||||||||||||||||||||||||||||
| 49 | one | ||||||||||||||||||||||||||||||||||||||||||
| 50 | two | ||||||||||||||||||||||||||||||||||||||||||
| 51 | three | ||||||||||||||||||||||||||||||||||||||||||
| 52 | four | ||||||||||||||||||||||||||||||||||||||||||
| 53 | five | ||||||||||||||||||||||||||||||||||||||||||
| 54 | six | ||||||||||||||||||||||||||||||||||||||||||
| 55 | seven | ||||||||||||||||||||||||||||||||||||||||||
| 56 | eight | ||||||||||||||||||||||||||||||||||||||||||
| 57 | nine | ||||||||||||||||||||||||||||||||||||||||||
| 58 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | my $TRIPLE = [ qw / | |||||||||||||||||||||||||||||||||||||||||
| 61 | mi | ||||||||||||||||||||||||||||||||||||||||||
| 62 | bi | ||||||||||||||||||||||||||||||||||||||||||
| 63 | tri | ||||||||||||||||||||||||||||||||||||||||||
| 64 | quadri | ||||||||||||||||||||||||||||||||||||||||||
| 65 | penti | ||||||||||||||||||||||||||||||||||||||||||
| 66 | hexi | ||||||||||||||||||||||||||||||||||||||||||
| 67 | septi | ||||||||||||||||||||||||||||||||||||||||||
| 68 | octi | ||||||||||||||||||||||||||||||||||||||||||
| 69 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 56 | ▶ | sub name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 56 | my $x = shift; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 56 | ▶ | $x = Math::BigInt -> new($x) unless ref($x); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 56 | my $class = ref($x); | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 56 | ▶ | return '' if $x -> is_nan(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 56 | my $ret = ''; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 56 | my $y = $x -> copy(); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 56 | my $rem; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 56 | ▶ | if ($y -> sign() eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | $ret = 'minus '; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | $y -> babs(); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 56 | ▶ | if ($y < 1000) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 45 | return $ret . $class -> _triple($y, 1, 0); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Split the number into numerical triplets. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 11 | my @num = (); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 11 | ▶ | while (!$y -> is_zero()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 25 | ($y, $rem) = $y -> bdiv(1000); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | unshift @num, $rem; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # Convert each numerical triplet into a string. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 11 | my @str = (); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 11 | for my $i (0 .. $#num) { | |||||||||||||||||||||||||||||||||||||||||
| 104 | 25 | my $num = $num[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 25 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 25 | my $index = $#num - $i; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 25 | my $count; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 25 | $count = $class -> _triple($num, 0, $i); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 25 | $str .= $count; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 25 | ▶ | if ($index > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 14 | my $triple_name = $class -> _triple_name($#num - $i, $num); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 14 | $str .= ' ' . $triple_name; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 25 | $str[$i] = $str; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # 1100 -> "one thousand one hundred" (not "one thousand and one hundred") | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # 1099 -> "one thousand and ninety-nine" (not "one thousand ninety-nine") | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # 1098 -> "one thousand and ninety-eight" (not "one thousand ninety-eight") | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # ... | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # 1001 -> "one thousand and one" (not "one thousand one") | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # 1000 -> "one thousand" (not "one thousand and zero") | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 11 | ▶ | if (@num > 1 && 0 < $num[-1] && $num[-1] < 100) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@num > 1 and 0 < $num[-1] and $num[-1] < 100
@num > 1 and 0 < $num[-1]
MC/DC66%2/3
@num > 1 and 0 < $num[-1] and $num[-1] < 100
@num > 1
0 < $num[-1]
$num[-1] < 100
Truth table75%3/4
A and B and C
A= @num > 1B= 0 < $num[-1]C= $num[-1] < 100
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | splice @str, -1, 0, "and"; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 11 | $ret . join(" ", grep /\S/, @str); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 49 | ▶ | sub _triple_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_triple_name: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 49 | my ($self, $index, $number) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | # index => 0 hundreds, tens and ones | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # index => 1 thousands | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # index => 2 millions | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 49 | ▶ | return '' if $index == 0 || $number -> is_zero(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index == 0 or $number->is_zero
MC/DC100%2/2
$index == 0 or $number->is_zero
$index == 0
$number->is_zero
Truth table100%3/3
A or B
A= $index == 0B= $number->is_zero
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 44 | ▶ | return 'thousand' if $index == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 35 | my $postfix = 'llion'; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 35 | my $plural = 's'; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 35 | ▶ | if (($index & 1) == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 16 | $postfix = 'lliard'; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 35 | ▶ | $postfix .= $plural unless $number -> is_one(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 35 | $index -= 2; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 35 | return $TRIPLE -> [$index >> 1] . $postfix; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 70 | ▶ | sub _triple { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_triple: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | # return name of a triple | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # input: number >= 0, < 1000 | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # only true if triple is the only triple | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 70 | my ($self, $number, $only) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # 0 => null, but only if there is just one triple | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 70 | ▶ | return '' if $number -> is_zero() && !$only; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$number->is_zero and not $only
MC/DC100%2/2
$number->is_zero and not $only
$number->is_zero
not $only
Truth table100%3/3
A and not(B)
A= $number->is_zeroB= not $only
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # we have the full name for these | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 65 | ▶ | return $SMALL -> [$number] if $number <= $#$SMALL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | # New code: | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 30 | my @num = (); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 30 | $num[1] = $number % 100; # tens and ones | |||||||||||||||||||||||||||||||||||||||||
| 169 | 30 | $num[0] = ($number - $num[1]) / 100; # hundreds | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 30 | my @str = (); | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Do the hundreds, if any. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 30 | ▶ | if ($num[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 18 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 18 | $str = $HUNDREDS -> [$num[0] - 1]; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 18 | $str .= " hundred"; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 18 | push @str, $str; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # Do the tens and ones, if any. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 30 | ▶ | if ($num[1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 19 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 19 | my $ones = $num[1] % 10; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 19 | my $tens = ($num[1] - $ones) / 10; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 19 | ▶ | if ($num[1] <= $#$SMALL) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 3 | $str = $SMALL -> [ $num[1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 16 | $str = $TENS -> [ $tens - 1]; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 16 | ▶ | if ($ones > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 15 | $str .= "-"; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 15 | $str .= $SMALL -> [ $ones ]; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 19 | push @str, $str; | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 30 | return join " and ", @str; | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | __END__ | ||||||||||||||||||||||||||||||||||||||||||