June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -20,6 +20,7 @@ function numtowords(n, minus,str) {
str = intowords(n)
}
gsub(/ /," ",str)
gsub(/ $/,"",str)
return(minus str)
}
function intowords(n) {

View file

@ -3,52 +3,56 @@ package main
import "fmt"
func main() {
for _, n := range []int64{12, 1048576, 9e18} {
fmt.Println(say(n))
}
for _, n := range []int64{12, 1048576, 9e18, -2, 0} {
fmt.Println(say(n))
}
}
var small = []string{"", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
var tens = []string{"ones", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"}
var illions = []string{"thousand", "million", "billion",
"trillion", "quadrillion", "quintillion"}
var small = [...]string{"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
var tens = [...]string{"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"}
var illions = [...]string{"", " thousand", " million", " billion",
" trillion", " quadrillion", " quintillion"}
func say(n int64) string {
switch {
case n < 1:
case n < 20:
return small[n]
case n < 100:
t := tens[n/10]
s := n % 10
if s > 0 {
t += " " + small[s]
}
return t
case n < 1000:
h := small[n/100] + " hundred"
s := n % 100
if s > 0 {
h += " " + say(s)
}
return h
default:
sx := say(n % 1000)
for i := 0; n >= 1000; i++ {
n /= 1000
p := n % 1000
if p > 0 {
ix := say(p) + " " + illions[i]
if sx > "" {
ix += " " + sx
}
sx = ix
}
}
return sx
}
return ""
var t string
if n < 0 {
t = "negative "
// Note, for math.MinInt64 this leaves n negative.
n = -n
}
switch {
case n < 20:
t += small[n]
case n < 100:
t += tens[n/10]
s := n % 10
if s > 0 {
t += "-" + small[s]
}
case n < 1000:
t += small[n/100] + " hundred"
s := n % 100
if s > 0 {
t += " " + say(s)
}
default:
// work right-to-left
sx := ""
for i := 0; n > 0; i++ {
p := n % 1000
n /= 1000
if p > 0 {
ix := say(p) + illions[i]
if sx != "" {
ix += " " + sx
}
sx = ix
}
}
t += sx
}
return t
}

View file

@ -1,46 +1,60 @@
public class Int2Words {
static String[] small = {"one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
static String[] tens = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
"ninety"};
static String[] big = {"thousand", "million", "billion", "trillion"};
public enum IntToWords {
;
private static final String[] small = {
"", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
private static final String[] tens = {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"};
private static final String[] big = {
"", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion"};
public static void main(String[] args) {
System.out.println(int2Text(900000001));
System.out.println(int2Text(1234567890));
System.out.println(int2Text(-987654321));
System.out.println(int2Text(0));
System.out.println(int2Text(10));
System.out.println(int2Text(30));
System.out.println(int2Text(47));
System.out.println(int2Text(100));
System.out.println(int2Text(999));
System.out.println(int2Text(1000));
System.out.println(int2Text(9999));
System.out.println(int2Text(123_456));
System.out.println(int2Text(900_000_001));
System.out.println(int2Text(1_234_567_890));
System.out.println(int2Text(-987_654_321));
System.out.println(int2Text(Long.MAX_VALUE));
System.out.println(int2Text(Long.MIN_VALUE));
}
public static String int2Text(long number) {
long num = 0;
String outP = "";
int unit = 0;
long tmpLng1 = 0;
StringBuilder sb = new StringBuilder();
if (number == 0) {
return "zero";
}
num = Math.abs(number);
long num = -Math.abs(number);
for (;;) {
tmpLng1 = num % 100;
if (tmpLng1 >= 1 && tmpLng1 <= 19) {
outP = small[(int) tmpLng1 - 1] + " " + outP;
} else if (tmpLng1 >= 20 && tmpLng1 <= 99) {
if (tmpLng1 % 10 == 0) {
outP = tens[(int) (tmpLng1 / 10) - 2] + " " + outP;
int unit = 1;
while (true) {
int rem100 = (int) -(num % 100);
if (rem100 >= 20) {
if (rem100 % 10 == 0) {
sb.insert(0, tens[rem100 / 10] + " ");
} else {
outP = tens[(int) (tmpLng1 / 10) - 2] + "-"
+ small[(int) (tmpLng1 % 10) - 1] + " " + outP;
sb.insert(0, tens[rem100 / 10] + "-" + small[rem100 % 10] + " ");
}
} else if (rem100 != 0) {
sb.insert(0, small[rem100] + " ");
}
tmpLng1 = (num % 1000) / 100;
if (tmpLng1 != 0) {
outP = small[(int) tmpLng1 - 1] + " hundred " + outP;
int hundreds = (int) -(num % 1000) / 100;
if (hundreds != 0) {
sb.insert(0, small[hundreds] + " hundred ");
}
num /= 1000;
@ -48,17 +62,17 @@ public class Int2Words {
break;
}
tmpLng1 = num % 1000;
if (tmpLng1 != 0) {
outP = big[unit] + " " + outP;
int rem1000 = (int) -(num % 1000);
if (rem1000 != 0) {
sb.insert(0, big[unit] + " ");
}
unit++;
}
if (number < 0) {
outP = "negative " + outP;
sb.insert(0, "negative ");
}
return outP.trim();
return sb.toString().trim();
}
}

View file

@ -0,0 +1,44 @@
const divMod = y => x => [Math.floor(y/x), y % x];
const sayNumber = value => {
let name = '';
let quotient, remainder;
const dm = divMod(value);
const units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
'seventy', 'eighty', 'ninety'];
const big = [...['', 'thousand'], ...['m', 'b', 'tr', 'quadr', 'quint',
'sext', 'sept', 'oct', 'non', 'dec'].map(e => `${e}illion`)];
if (value < 0) {
name = `negative ${sayNumber(-value)}`
} else if (value < 20) {
name = units[value]
} else if (value < 100) {
[quotient, remainder] = dm(10);
name = `${tens[quotient]} ${units[remainder]}`.replace(' zero', '');
} else if (value < 1000) {
[quotient, remainder] = dm(100);
name = `${sayNumber(quotient)} hundred and ${sayNumber(remainder)}`
.replace(' and zero', '')
} else {
const chunks = [];
const text = [];
while (value !== 0) {
[value, remainder] = divMod(value)(1000);
chunks.push(remainder);
}
chunks.forEach((e,i) => {
if (e > 0) {
text.push(`${sayNumber(e)}${i === 0 ? '' : ' ' + big[i]}`);
if (i === 0 && e < 100) {
text.push('and');
}
}
});
name = text.reverse().join(', ').replace(', and,', ' and');
}
return name;
};