js-written-number icon indicating copy to clipboard operation
js-written-number copied to clipboard

Fractions and non-integer numbers

Open tomByrer opened this issue 9 years ago • 9 comments

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?

tomByrer avatar Apr 17 '15 04:04 tomByrer

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

maxiruani avatar Apr 24 '15 01:04 maxiruani

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

yamadapc avatar Apr 24 '15 01:04 yamadapc

I feel this is lower priority

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

tomByrer avatar Apr 24 '15 02:04 tomByrer

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.

tomByrer avatar May 09 '15 05:05 tomByrer

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

juhana avatar Jul 11 '16 00:07 juhana

@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'

yamadapc avatar Jul 11 '16 13:07 yamadapc

Thanks for the fast patch!

juhana avatar Jul 11 '16 13:07 juhana

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.

Mlallena avatar Oct 19 '22 09:10 Mlallena