Skip to content

Support hexadecimal, octal and binary numbers in $number() #305

Closed
@ck-c8y

Description

@ck-c8y

Hi,

I would like to use the $number() function to decode some hexadecimal numbers.

The current implementation does not support this, e.g.
$number('0x575')
throws an exception:
com.api.jsonata4java.expressions.EvaluateRuntimeException: Unable to cast value to a number: "0x575"

The JSONata $number() function supports this and returns 1397.

Would it be possible to add some code like the following to the current implementation in NumberUtils.java:

public static int convertToInteger(String number) {
        if (number.startsWith("0x")) {
            // Hexadecimal number
            return Integer.parseInt(number.substring(2), 16);
        } else if (number.startsWith("0o")) {
            // Octal number
            return Integer.parseInt(number.substring(2), 8);
        } else if (number.startsWith("0b")) {
            // Binary number
            return Integer.parseInt(number.substring(2), 2);
        } else {
            // Decimal number or invalid format
            return Integer.parseInt(number);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions