Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free cross-platform library to convert numbers (money amounts) to words? [closed]

I'm looking for cross-platform library which I can use in my C application to convert money amounts (e.g. $123.50) to words (one hundred twenty three dollars and fifty cents). I need support for multiple currencies: dollars, euros, UK pounds etc.

Although I understand this is not hard at all to write my own implementation, but I'd like to avoid reinventing wheel. I've tried to google it, but there is too much noise related to MS Word converters.

Can anybody suggest something?

UPDATE numerous comments suggest to write my own implementation because it's really easy task. And I agree. My point was about support of multiple currencies in the same time and different business rules to spell the amounts (should be fractional part written as text or numbers? etc.) As I understand serious business applications have such library inside, but I think there is nothing open-source available, maybe because it seems as very easy task.

I'm going to write my own libary and then open-source it. Thanks to all.

like image 644
bialix Avatar asked Dec 23 '10 14:12

bialix


2 Answers

The ICU has exactly this, and an API to build your own rules for number->text, for spelling out. See here.

like image 140
Dervin Thunk Avatar answered Sep 17 '22 19:09

Dervin Thunk


Are you looking for something like this? It's not a complete solution as it externalizes the handling of the currency. Furthermore, it fails for negative numbers.

Just allocate a character buffer and call write_number. The third and fourth argument are expected to be the currency units (in their plural form), e.g. "dollars" and "cents". (The singular case isn't properly handled yet.

#include <math.h>
#include <string.h>

const char* SMALL_NUMBERS[] = {
    0, "one ", "two ", "three ", "four ",
    "five ", "six ", "seven ", "eight ", "nine ",
    "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ",
    "fiftenn ", "sixteen ", "seventeen ", "eighteen ", "nineteen "
};

const char* TENS[] = {
    0, 0, "twenty ", "thirty ", "forty ",
    "fifty ", "sixty ", "seventy ", "eighty ", "ninety "
};


void append_lt_1000(char* buf, int num)
{
    if (num >= 100) {
        strcat(buf, SMALL_NUMBERS[num / 100]);
        strcat(buf, "hundred ");
        num %= 100;
    }
    if (num >= 20) {
        strcat(buf, TENS[num / 10]);
        num %= 10;
    }
    if (num != 0)
        strcat(buf, SMALL_NUMBERS[num]);
}

void append_mag(char* buf, double* number, double magnitude, const char* mag_name)
{
    if (*number < magnitude)
        return;

    append_lt_1000(buf, (int)(*number / magnitude));
    strcat(buf, mag_name);
    *number = fmod(*number, magnitude);
}

void write_number(char* buf, double number, const char* major_unit, const char* minor_unit)
{
    double ip, fp;

    buf[0] = 0;
    fp = modf(number, &ip);

    if (ip == 0) {
        strcat(buf, "zero ");
    } else {
        append_mag(buf, &ip, 1000000000000.0, "trillion ");
        append_mag(buf, &ip, 1000000000.0, "billion ");
        append_mag(buf, &ip, 1000000, "million ");
        append_mag(buf, &ip, 1000, "thousand ");
        append_lt_1000(buf, (int)ip);
    }
    strcat(buf, major_unit);

    if (fp != 0) {
        strcat(buf, " and ");
        append_lt_1000(buf, (int)(fp * 100));
        strcat(buf, minor_unit);
    }
}
like image 31
Codo Avatar answered Sep 21 '22 19:09

Codo