Hi,

While looking at formatting.c file, I noticed a TODO about "add support for
roman number to standard number conversion"  (
https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/formatting.c#L52
)

I have attached the patch that adds support for this and converts roman
numerals to standard numbers (1 to 3999) while also checking if roman
numerals are valid or not.
I have also added a new error code: ERRCODE_INVALID_ROMAN_NUMERAL in case
of invalid numerals.

A few examples:

postgres=# SELECT to_number('MC', 'RN');
 to_number
-----------
      1100
(1 row)

postgres=# SELECT to_number('XIV', 'RN');
 to_number
-----------
        14
(1 row)

postgres=# SELECT to_number('MMMCMXCIX', 'RN');
 to_number
-----------
      3999
(1 row)

postgres=# SELECT to_number('MCCD', 'RN');
ERROR:  invalid roman numeral

I would appreciate your feedback on the following cases:
- Is it okay for the function to handle Roman numerals in a
case-insensitive way? (e.g., 'XIV', 'xiv', and 'Xiv' are all seen as 14).
- How should we handle Roman numerals with leading or trailing spaces, like
' XIV' or 'MC '? Should we trim the spaces, or would it be better to throw
an error in such cases?

I have tested the changes and would appreciate any suggestions for
improvement. I will update the docs and regressions in the next version of
patch.

Regards,
Hunaid Sohail

Attachment: v1-0001-Add-RN-rn-support-for-to_number-function.patch
Description: Binary data

Reply via email to