fix: ordinal_num returns '1.0st' for float 1.0 instead of '1st'#666
Open
gaoflow wants to merge 1 commit into
Open
fix: ordinal_num returns '1.0st' for float 1.0 instead of '1st'#666gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
num2words(1.0, to='ordinal_num') returned '1.0st' instead of '1st' because verify_ordinal accepts floats equal to their int value but each language's to_ordinal_num used str() on the raw float, embedding the decimal point in the result. The same bug affected to_ordinal_num in all ~20 language implementations. Fix by casting to int in the num2words dispatcher when the conversion type is 'ordinal' or 'ordinal_num' and the value is a float that equals its integer truncation. This restores correct results in a single place without touching every language module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
num2words(1.0, to='ordinal_num')returns'1.0st'instead of'1st'.The same problem affects every language that implements
to_ordinal_num.Root cause
verify_ordinalaccepts floats that equal their integer value (1.0 == int(1.0)),so non-integer floats are correctly rejected while integer-valued floats pass through.
However, every
to_ordinal_numimplementation then convertsvalueto a string withstr(value)or"%s" % value, which for1.0yields"1.0", embedding the decimalpoint in the result.
Fix
Cast integer-valued floats to
intinsidenum2words()before dispatching to thelanguage converter, for
to='ordinal'andto='ordinal_num'. This fixes all languageimplementations in one place without touching every language module.
Non-integer floats (
1.5) still raiseTypeErroras before.