Skip to content

Instantly share code, notes, and snippets.

@strindhaug
Created September 11, 2016 10:28
Show Gist options
  • Save strindhaug/7aa7e6d6f9ffafdd28957c0ac8c50b59 to your computer and use it in GitHub Desktop.
Save strindhaug/7aa7e6d6f9ffafdd28957c0ac8c50b59 to your computer and use it in GitHub Desktop.
Print numbers as words in Norwegian
words = {
0: ""
1: "en"
2: "to"
3: "tre"
4: "fire"
5: "fem"
6: "seks"
7: "sju"
8: "åtte"
9: "ni"
10: "ti"
11: "elleve"
12: "tolv"
13: "tretten"
14: "fjorten"
15: "femten"
16: "seksten"
17: "søtten"
18: "atten"
19: "nitten"
20: "tjue"
30: "tretti"
40: "førti"
50: "femti"
60: "seksti"
70: "søtti"
80: "åtti"
90: "nitti"
}
TUSEN = 1000
MILLION = 1000 * TUSEN
MILLIARD = 1000 * MILLION
BILLION = 1000 * MILLIARD
bigwords = [
{
val: BILLION
threshold: BILLION
needsand: MILLIARD
unit: "en billion"
unitp: "billioner"
neuter: false
# max: 1000 * MILLIARD
}
{
val: MILLIARD
threshold: MILLIARD
needsand: MILLION
unit: "en milliard"
unitp: "milliarder"
neuter: false
# max: 1000 * MILLIARD
}
{
val: MILLION
threshold: MILLION
needsand: MILLION / 1000
unit: "en million"
unitp: "millioner"
neuter: false
# max: MILLIARD
}
{
val: TUSEN
threshold: 1000
needsand: 100
unit: "ett tusen"
unitp: "tusen"
neuter: true
# max: MILLION
}
{
val: 100
threshold: 100
needsand: 100
unit: "ett hundre"
unitp: "hundre"
neuter: true
# max: TUSEN
}
]
MAX = 900 * BILLION
numberToNorwegian = (tall, initial=true) ->
tall = parseInt(tall)
ord = []
if tall < 0
ord.push("minus")
tall = -tall
if tall > MAX
return tall
if initial and tall is 0
return "null"
for conf in bigwords
if tall >= conf.threshold
low = tall % conf.val
high = parseInt(tall / conf.val)
if high > 1
ord.push(numberToNorwegian(high, false))
ord.push(conf.unitp)
else
ord.push(conf.unit)
tall = low
if tall < (conf.needsand) and tall > 0
ord.push("og")
if tall < 1
return ord.join(" ")
if tall < 20
ord.push(words[tall])
else if tall < 100
ones = tall % 10
tens = tall - ones
ord.push(words[tens] + words[ones])
return ord.join(" ")
tests = [
88888888
88088888
88008888
88000888
100010001000
10000000000000
40952067984745
]
for t in tests
console.log(t + " = " + numberToNorwegian(t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment