js-written-number
js-written-number copied to clipboard
Fractions and non-integer numbers
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?
Maybe it can be handle to give support to fractions and ordinal numbers too. This would be a nice feature.
I feel this is lower priority, but let's do it!
I feel this is lower priority
I also. Could be something to work on piece by piece.
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.
To start with, it would be nice if it could handle non-integer input without stack overflow.
@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'
Thanks for the fast patch!
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.