-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.h
44 lines (40 loc) · 901 Bytes
/
convert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string.h>
#include <stdio.h>
char *digits[] = {
"one","two","three","four","five", "six","seven","eight","nine",
"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen"
};
char *tens[] = {
"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
};
void three_digits(char *str, unsigned int v)
{
if (v > 99) {
strcat(str,digits[v/100-1]);
strcat(str,"hundred");
v %= 100;
}
if (v > 19) {
strcat(str,tens[v/10-2]);
v %= 10;
}
if (v > 0)
strcat(str,digits[v-1]);
}
void number_to_words(char *str, unsigned int v)
{
strcpy(str,"");
if (v > 999999) {
three_digits(str, v/1000000);
strcat(str, "million");
v %= 1000000;
}
if (v > 999) {
three_digits(str, v/1000);
strcat(str, "thousand");
v %= 1000;
}
three_digits(str,v);
}
const int max_words_length = 88; // eg. # 377377377 + a null