yamadapc / js-written-number

Convert numbers to words - their written form
https://yamadapc.github.io/js-written-number
MIT License
366 stars 124 forks source link

Fractions and non-integer numbers #2

Open tomByrer opened 9 years ago

tomByrer commented 9 years ago

Neat lib, thanks for the clean & easy to scan code!

it('correctly converts numbers < 0', function() {
  writtenNumber(0.5).should.equal('half');
  writtenNumber('1/3').should.equal('a third'); // one third?
  writtenNumber('0.333333333').should.equal('a third'); // one third?
  writtenNumber('0.1').should.equal('a tenth'); // one tenth?
  writtenNumber('1/16').should.equal('a sixteenth');
});

Might be out of scope?

maxiruani commented 9 years ago

Maybe it can be handle to give support to fractions and ordinal numbers too. This would be a nice feature.

yamadapc commented 9 years ago

I feel this is lower priority, but let's do it!

tomByrer commented 9 years ago

I feel this is lower priority

I also. Could be something to work on piece by piece.

tomByrer commented 9 years ago

Here are some more scenarios that I'm not sure what to do with, what do you think?

it('correctly converts numbers < 0', function() {
  writtenNumber(0.49).should.equal('half');  //rounding?

  writtenNumber('0.01').should.equal('zero decimal zero one'); // or "one hundredth"?
  writtenNumber('1/3').should.equal('zero point three three three three three three');
//^ not "a third", or as additional option??
});

Saying "one half" is more conversational for prose. But "zero [point|decimal] five" is more like text-to-speech or precise for banking.

juhana commented 8 years ago

To start with, it would be nice if it could handle non-integer input without stack overflow.

yamadapc commented 8 years ago

@juhana Just patched to round non-integer input. Anything else should result in the empty string. Thanks for reporting the error being thrown.

This function should not throw errors unless we throw them.

Version 0.3.0 rounds numbers before moving on, so:

writtenNumber('0.01') // => 'zero'
writtenNumber('0.8')  // => 'one'
juhana commented 8 years ago

Thanks for the fast patch!

Mlallena commented 2 years ago

My suggestion for this would be to add two new elements to the JSON files:

Then, you would just have to split the number down the decimal point (assuming we're doing this with characters), apply the normal function to both sides and then glue them with the decimalWord.

Of course, this runs into the problem of what would happen if it was a number with one or more zeros after the decimal point, like "7.05"...

EDIT: Solved. I added a line that takes the zeros after the point and adds as many "zero"s as needed before the rest of the numbers.

abdelouahabb commented 1 year ago

My suggestion for this would be to add two new elements to the JSON files:

  • "decimal": the character used to mark the decimal point (most languages use ".", but some use ",").
  • "decimalWord": the word used to mark the decimal point (for example, in English "12.5" would be "twelve point five").

Then, you would just have to split the number down the decimal point (assuming we're doing this with characters), apply the normal function to both sides and then glue them with the decimalWord.

Of course, this runs into the problem of what would happen if it was a number with one or more zeros after the decimal point, like "7.05"...

EDIT: Solved. I added a line that takes the zeros after the point and adds as many "zero"s as needed before the rest of the numbers.

Here is the trick i did on my code (using French), and final is the float value, I only use .toFixed(2)

if (final.toString().split(".").length > 1)
        {
            if (final.toString().split(".")[1].startsWith('0'))
            {
                $("#resultfinal").html(`${writtenNumber(parseInt(final.toString().split(".")[0]), {lang: 'fr'})} virgule Zero ${writtenNumber(parseInt(final.toString().split(".")[1]), {lang: 'fr'})}`)
            }
            else
            {
                $("#resultfinal").html(`${writtenNumber(parseInt(final.toString().split(".")[0]), {lang: 'fr'})} virgule ${writtenNumber(parseInt(final.toString().split(".")[1]), {lang: 'fr'})}`)
            }
        }
        else
        {
        $("#resultfinal").html(`${writtenNumber(final, {lang: 'fr'})}`)
        }