| 1 | # -*- mode: perl; -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Math::BigInt::Named::Norwegian; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Math::BigInt::Named; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | our @ISA = qw< Math::BigInt::Named >; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | my $SMALL = [ qw/ | |||||||||||||||||||||||||||||||||||||||||
| 14 | null | ||||||||||||||||||||||||||||||||||||||||||
| 15 | en | ||||||||||||||||||||||||||||||||||||||||||
| 16 | to | ||||||||||||||||||||||||||||||||||||||||||
| 17 | tre | ||||||||||||||||||||||||||||||||||||||||||
| 18 | fire | ||||||||||||||||||||||||||||||||||||||||||
| 19 | fem | ||||||||||||||||||||||||||||||||||||||||||
| 20 | seks | ||||||||||||||||||||||||||||||||||||||||||
| 21 | syv | ||||||||||||||||||||||||||||||||||||||||||
| 22 | åtte | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ni | ||||||||||||||||||||||||||||||||||||||||||
| 24 | ti | ||||||||||||||||||||||||||||||||||||||||||
| 25 | elleve | ||||||||||||||||||||||||||||||||||||||||||
| 26 | tolv | ||||||||||||||||||||||||||||||||||||||||||
| 27 | tretten | ||||||||||||||||||||||||||||||||||||||||||
| 28 | fjorten | ||||||||||||||||||||||||||||||||||||||||||
| 29 | femten | ||||||||||||||||||||||||||||||||||||||||||
| 30 | seksten | ||||||||||||||||||||||||||||||||||||||||||
| 31 | sytten | ||||||||||||||||||||||||||||||||||||||||||
| 32 | atten | ||||||||||||||||||||||||||||||||||||||||||
| 33 | nitten | ||||||||||||||||||||||||||||||||||||||||||
| 34 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | my $TENS = [ qw / | |||||||||||||||||||||||||||||||||||||||||
| 37 | ti | ||||||||||||||||||||||||||||||||||||||||||
| 38 | tjue | ||||||||||||||||||||||||||||||||||||||||||
| 39 | tretti | ||||||||||||||||||||||||||||||||||||||||||
| 40 | førti | ||||||||||||||||||||||||||||||||||||||||||
| 41 | femti | ||||||||||||||||||||||||||||||||||||||||||
| 42 | seksti | ||||||||||||||||||||||||||||||||||||||||||
| 43 | sytti | ||||||||||||||||||||||||||||||||||||||||||
| 44 | åtti | ||||||||||||||||||||||||||||||||||||||||||
| 45 | nitti | ||||||||||||||||||||||||||||||||||||||||||
| 46 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my $HUNDREDS = [ qw / | |||||||||||||||||||||||||||||||||||||||||
| 49 | en | ||||||||||||||||||||||||||||||||||||||||||
| 50 | to | ||||||||||||||||||||||||||||||||||||||||||
| 51 | tre | ||||||||||||||||||||||||||||||||||||||||||
| 52 | fire | ||||||||||||||||||||||||||||||||||||||||||
| 53 | fem | ||||||||||||||||||||||||||||||||||||||||||
| 54 | seks | ||||||||||||||||||||||||||||||||||||||||||
| 55 | sju | ||||||||||||||||||||||||||||||||||||||||||
| 56 | åtte | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ni | ||||||||||||||||||||||||||||||||||||||||||
| 58 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | my $TRIPLE = [ qw / | |||||||||||||||||||||||||||||||||||||||||
| 61 | mi | ||||||||||||||||||||||||||||||||||||||||||
| 62 | bi | ||||||||||||||||||||||||||||||||||||||||||
| 63 | tri | ||||||||||||||||||||||||||||||||||||||||||
| 64 | kvadri | ||||||||||||||||||||||||||||||||||||||||||
| 65 | kvinti | ||||||||||||||||||||||||||||||||||||||||||
| 66 | seksti | ||||||||||||||||||||||||||||||||||||||||||
| 67 | septi | ||||||||||||||||||||||||||||||||||||||||||
| 68 | okti | ||||||||||||||||||||||||||||||||||||||||||
| 69 | noni | ||||||||||||||||||||||||||||||||||||||||||
| 70 | desi | ||||||||||||||||||||||||||||||||||||||||||
| 71 | undesi | ||||||||||||||||||||||||||||||||||||||||||
| 72 | duodesi | ||||||||||||||||||||||||||||||||||||||||||
| 73 | tredesi | ||||||||||||||||||||||||||||||||||||||||||
| 74 | kvattordesi | ||||||||||||||||||||||||||||||||||||||||||
| 75 | kvindesi | ||||||||||||||||||||||||||||||||||||||||||
| 76 | seksdesi | ||||||||||||||||||||||||||||||||||||||||||
| 77 | septendesi | ||||||||||||||||||||||||||||||||||||||||||
| 78 | oktodesi | ||||||||||||||||||||||||||||||||||||||||||
| 79 | novemdesi | ||||||||||||||||||||||||||||||||||||||||||
| 80 | viginti | ||||||||||||||||||||||||||||||||||||||||||
| 81 | / ]; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 55 | ▶ | sub name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 55 | my $x = shift; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 55 | ▶ | $x = Math::BigInt -> new($x) unless ref($x); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 55 | my $class = ref($x); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 55 | ▶ | return '' if $x -> is_nan(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 55 | my $ret = ''; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 55 | my $y = $x -> copy(); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 55 | my $rem; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 55 | ▶ | if ($y -> sign() eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $ret = 'minus '; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $y -> babs(); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 55 | ▶ | if ($y < 1000) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 42 | return $ret . $class -> _triple($y, 1, 0); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | # Split the number into numerical triplets. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 13 | my @num = (); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 13 | ▶ | while (!$y -> is_zero()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 32 | ($y, $rem) = $y -> bdiv(1000); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 32 | unshift @num, $rem; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # Convert each numerical triplet into a string. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 13 | my @str = (); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 13 | for my $i (0 .. $#num) { | |||||||||||||||||||||||||||||||||||||||||
| 116 | 32 | my $num = $num[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 32 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 32 | my $index = $#num - $i; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 32 | my $count; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 32 | ▶ | if ($num == 1 && $index == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$num == 1 and $index == 1
MC/DC100%2/2
$num == 1 and $index == 1
$num == 1
$index == 1
Truth table100%3/3
A and B
A= $num == 1B= $index == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 8 | $count = "ett"; # "ett tusen", not "en tusen" | |||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 24 | $count = $class -> _triple($num, 0, $i); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 32 | $str .= $count; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # "tusen", "million"/"millioner", "milliard/milliarder", ... | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 32 | ▶ | if ($index > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 19 | my $triple_name = $class -> _triple_name($#num - $i, $num); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 19 | $str .= ' ' . $triple_name; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 32 | $str[$i] = $str; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # 1100 -> "ett tusen ett hundre" (not "ett tusen og ett hundre") | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # 1099 -> "ett tusen og nittini" (not "ett tusen nittini") | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # 1098 -> "ett tusen og nittiåtte" (not "ett tusen nittiåtte") | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # ... | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # 1001 -> "ett tusen og en" (not "ett tusen en") | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # 1000 -> "ett tusen" (not "ett tusen og null" | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 13 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 5 | splice @str, -1, 0, "og"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 13 | $ret . join(" ", grep /\S/, @str); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 19 | ▶ | sub _triple_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_triple_name: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 19 | my ($self, $index, $number) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 154 | # index => 0 hundreds, tens and ones | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # index => 1 thousands | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # index => 2 millions | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 19 | ▶ | return '' if $index == 0 || $number -> is_zero(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index == 0 or $number->is_zero
MC/DC50%1/2
$index == 0 or $number->is_zero
$index == 0
$number->is_zero
Truth table66%2/3
A or B
A= $index == 0B= $number->is_zero
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 14 | ▶ | return 'tusen' if $index == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | my $postfix = 'llion'; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | my $plural = 'er'; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | ▶ | if (($index & 1) == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | $postfix = 'lliard'; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | ▶ | $postfix .= $plural unless $number -> is_one(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | $index -= 2; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | $TRIPLE -> [$index >> 1] . $postfix; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 66 | ▶ | sub _triple { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_triple: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | # return name of a triple | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # input: number >= 0, < 1000 | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # only true if triple is the only triple | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 66 | my ($self, $number, $only) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # 0 => null, but only if there is just one triple | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 66 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | # we have the full name for these | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 58 | ▶ | return $SMALL -> [$number] if $number <= $#$SMALL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # New code: | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 29 | my @num = (); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 29 | $num[1] = $number % 100; # tens and ones | |||||||||||||||||||||||||||||||||||||||||
| 187 | 29 | $num[0] = ($number - $num[1]) / 100; # hundreds | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 29 | my @str = (); | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | # Do the hundreds, if any. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 29 | ▶ | if ($num[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 17 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 17 | ▶ | $str = $num[0] == 1 ? "ett" # "ett hundre", not "en hundre" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | : $HUNDREDS -> [$num[0] - 1]; | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 17 | $str .= " hundre"; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 17 | push @str, $str; | |||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | # Do the tens and ones, if any. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 29 | ▶ | if ($num[1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 18 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 18 | my $ones = $num[1] % 10; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 18 | my $tens = ($num[1] - $ones) / 10; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 18 | ▶ | if ($num[1] <= $#$SMALL) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 4 | $str = $SMALL -> [ $num[1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 14 | $str = $TENS -> [ $tens - 1]; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 14 | ▶ | if ($ones > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 13 | $str .= ""; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 13 | $str .= $SMALL -> [ $ones ]; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 18 | push @str, $str; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 29 | return join " og ", @str; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | __END__ | ||||||||||||||||||||||||||||||||||||||||||