Jump to content
Attila Kovacs

is there any "currency to words" routine for Delphi?

Recommended Posts

39 minutes ago, Attila Kovacs said:

including torry

And what doesn't satisfy you with the NumWords v.4.6 result?

 

(Worked fine for me)

 

1155600418_2019-09-2414_33_39-Window.png.7770fef83a8eb2c485b811bc89ca6540.png

Edited by rvk
  • Like 2

Share this post


Link to post
35 minutes ago, rvk said:

And what doesn't satisfy you with the NumWords v.4.6 result?

Rule 2a. Hyphenate all compound numbers from twenty-one through ninety-nine.

Rule 8b. When writing out numbers above 999, do not use commas.

for the first sight. But I could take this as a base if there is no fully correct lib out of the box.

Let's see what other people think.

Share this post


Link to post
9 minutes ago, Attila Kovacs said:

Rule 2a. Hyphenate all compound numbers from twenty-one through ninety-nine.

Rule 8b. When writing out numbers above 999, do not use commas.

You mentioned a "great solution on a website". https://www.calculator.org/calculate-online/mathematics/text-number.html

It translates 2019 to "two thousand, nineteen"

How does that one follow those rules?

 

But maybe there are better ones.

But I haven't even seen any ones online that convert according to those rules.

(or maybe this one https://www.tools4noobs.com/online_tools/number_spell_words/)

Edited by rvk

Share this post


Link to post

We did something like this in a commercial application: it only handles numbers up to 9999 but in multiple languages if I'm not mistaken.

To be completely honest, it's quicker just to write your own from scratch which will satisfy your needs rather than to look for one. The code I was talking about is about 50-100 lines in Delphi...

 

Due to it's commercial nature I'll not be able to share sniplets, but it's basically these rules in lots of "if" statements 🙂

  • Like 1

Share this post


Link to post
4 minutes ago, aehimself said:

The code I was talking about is about 50-100 lines in Delphi...

Correct. The base of the code from NumWords is just 70-somewhat lines and can easily be adjusted to follow the additional/proper rules.

 

const
  Digits: array [1 .. 9] of string = (
    'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');

  Teens: array [1 .. 9] of string = (
    'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen');

  TenTimes: array [1 .. 9] of string = (
    'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety');

function DoTriplet(TheNumber: Integer): string;
var
  Digit, Num: Integer;
begin
  Result := '';
  Num := TheNumber mod 100;
  if (Num > 10) and (Num < 20) then
  begin
    Result := Teens[Num - 10];
    Num := TheNumber div 100;
  end
  else
  begin
    Num := TheNumber;
    Digit := Num mod 10;
    Num := Num div 10;
    if Digit > 0 then Result := Digits[Digit];
    Digit := Num mod 10;
    Num := Num div 10;
    if Digit > 0 then Result := TenTimes[Digit] + ' ' + Result;
    Result := Trim(Result);
  end;
  Digit := Num mod 10;
  if (Result <> '') and (Digit > 0) then Result := 'and ' + Result;
  if Digit > 0 then Result := Digits[Digit] + ' hundred ' + Result;
  Result := Trim(Result);
end;

function NumberInWords(TheNumber: Integer): string;
var
  Num, Triplet, Pass: Integer;
begin
  if TheNumber < 0 then Result := 'Minus ' + NumberInWords(-TheNumber)
  else
  begin
    Result := '';
    Num := TheNumber;
    if Num > 999999999 then
        raise Exception.Create('Can''t express more than 999,999,999 in words');
    for Pass := 1 to 3 do
    begin
      Triplet := Num mod 1000;
      Num := Num div 1000;
      if Triplet > 0 then
      begin
        if (Pass > 1) and (Result <> '') then Result := ', ' + Result;
        case Pass of
          2: Result := ' thousand' + Result;
          3: Result := ' million' + Result;
        end;
        Result := Trim(DoTriplet(Triplet) + Result);
      end;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Showmessage(NumberInWords(2120229));
end;


 

  • Like 1

Share this post


Link to post
    if Num > 999999999 then
        raise Exception.Create('Can''t express more than 999,999,999 in words');

This made me giggle 🙂

Seriously though, ours is somewhat similar. It's not this sophisticated though.

  • Haha 1

Share this post


Link to post

I think that depends a lot on what languages you are targeting.

See i18n for howto handle different pluralities in other languages well, this can get very scaring.

  • Like 1
  • Thanks 1

Share this post


Link to post

Yeeah, when you move from English-only to multi-language, these 100 lines will turn into 100 files xD

  • Like 1

Share this post


Link to post
13 hours ago, Fr0sT.Brutal said:

Yeeah, when you move from English-only to multi-language, these 100 lines will turn into 100 files xD

Well, I'd say one language = one set of of rules = one helper unit. You'll not reach 100 files, but can get close if you are ambitious enough 🙂

 

Plus, you need a fluent speaker of that language to help you out. I personally do not speak French, but I hear they have quite messed up way of saying numbers (like 92 is "76 and 16" or something).

  • Confused 1

Share this post


Link to post

Just to illustrate the complexity of this in a different language, look at the danes that work with n times 20s and half-20s when above 50 and below 100, and they are not entirely consistent about it either.

I am not a native dane, so there may be some mistakes - but this is my understanding of their classic spoken numbering.
 

førr = forty = 40

fem og førr = five and forty = 45

half tres / femti = fifty = 3 x 20 - 10 = 50

seks og halv tres = six and fifty =  6 + (3 x 20 - 10) = 56

seksti / tres = sixty = 3 x 20 = 60

fem og seksti = five and sixty = 65

half firs / søtti = seventy = 4 x 20 - 10 = 70 

åtti / firs = eighty = 4 x 20 = 80

half fems / nitti = ninety = 5 x 20 - 10 = 90

syv og halv fems = seven and ninety = 97

hundrede / fems = hundred = 5 x 20 = 100 

 

277 = to hundrede og syv og halv firs = two hundred and seven and seventy = 200 + 7 + 70

 

Modern Danish does allow saying 55 = "femti fem" instead of "fem og halv tres" - i.e. more like the English spoken "fifty five", but the classic form is widely used.

Modern 277 would be "to hundrede og søtti syv", like the English "two hundred and seventy seven".

  • Confused 1

Share this post


Link to post
10 hours ago, aehimself said:

Well, I'd say one language = one set of of rules = one helper unit. You'll not reach 100 files, but can get close if you are ambitious enough

Huh, ICU lib  contains pretty much more than 100 files and that is even not considering data, only the code! Of course ICU is global but proper int-tion isn't piece a cake

 

3 minutes ago, Lars Fosdal said:

half tres / femti = fifty = 3 x 20 - 10 = 50

O___O Wow, so weird! Didn't know that.

Edited by Fr0sT.Brutal

Share this post


Link to post
 
 
 
1
3 minutes ago, Fr0sT.Brutal said:

O___O Wow, so weird! Didn't know that.

You could also say that the half indicates a half twenty.  

 

half tres = 2.5 x 20 = 50

half firs = 3.5 x 20 = 70

half fems = 4.5 x 20 = 90

 

It is logical, but to a foreigner it does feel a bit jumbled - and why isn't there two twenty somethings like, 1.5 x 20 = 30, and 2 x 20 = 40?

  • Haha 1

Share this post


Link to post
18 hours ago, aehimself said:

Plus, you need a fluent speaker of that language to help you out.

Oh, yes you do! That is one of the very few constants, like 0 degrees Kevin.

7 hours ago, Lars Fosdal said:

seksti

Never heard a Dane say "sexti". Or did you mix Norwegian and Danish? The inconsistency that always throws me off phonetically is "firs" and "fjers". It has been explained to me numerous times by danes, but not consistently 🙂

 

Some other nuggets of (in this context quite useless information):

 

The Danish way of doing numbers is so difficult that some schools have started teaching what they dub "Swedish Math". More or less like english. It's a bit frown upon but those kids excel in math compared to others.

 

Danish (the language) is considered "muddy", phonetically challenging, because they tend to "swallow" the last syllable in each word. The native speakers shift between articulating more and "swallowing" more. This shifts happen in a sinuous way where the cycles are around 20 years. Some researcher concluded that kids that grow up in times when the trend is muddy are 2 years behind in their language development. I do not have a link, so this is kind of hearsay. Sorry. And the great thing about it is that us developers mostly do not have to care (if you are not into NLP i danish - thank... whoever for not putting me in that) because orthographically danish is really quite ok. It is a beautiful language IMHO. Abundant with top-of-the shelf idioms and proverbs.

 

There are danes here (@delphipraxis). So i am now jumping down into a trench 😎 helmet on.

 

- - -

 

When the editor gets "stuck", just realised you refresh the page and click your comment line again (what you wrote pops back up) and it works again!!

  • Haha 1

Share this post


Link to post

@Dany Marmur - I'm not a Dane, so it may be that the "seksti" / sixty is purely Norwegian.

The standing Norwegian joke about the Danish language is that it is not a language, but a throat disease 😛

That said, Norwegian was under Danish rule for hundreds of years, so the languages are quite close in structure as well as vocabulary. 

Norwegian is "harder" / "crisper" in pronunciation, though - unless you consider the south-most dialects, neighboring Denmark 🙂

Share this post


Link to post

@Lars Fosdal, being here and previously in G+ for a long, it would be impossible for this language-freak swede (me) not to know that!

We are getting seriously OT here... - perhaps a sub-forum called "Natural Language (Processing and Discussions)" could be helpful?

"Coders love language, absolute and natural."

  • Like 1

Share this post


Link to post

You might also consider the french 77 or 99, which are expressed as:

  • 77 -> soixante-dixsept (60 + 17)
  • 99 -> quatrevingt-dixneuf (4 * 20 + 19)

as a nice complicated example

 

From a German point of view, Swiss and Dutch sound like someone has a bad cold, but danish isn't far behind. GD&R.

Share this post


Link to post

It seems that the French has a "times 20" system that is somewhat similar to the Danes.

Share this post


Link to post

Russian also has legacy naming of 11..19: 11 is odinnadzat = odin-na-desyat = one-and-ten while numbers above 20 are usual "most significant digit first" like 21 is dvadzat odin = twenty one. And all numers that are multiple of 10 are named in the same logical manner like 30 is tridzat = tri-desyat = three tens except 40 which is just weird "sorok".

Well, even the strict and logical English has 11 and 12 with their own personal names, maybe it's a legacy of 12-al system?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×