Skip to content

Conversation

@asifabashar
Copy link

Description

tonumber(, )
This function converts a string to a number.
Usage
You can use this function with the eval and where commands, in the WHERE clause of the from command, and as part of evaluation expressions with other commands.
The argument can be a string or the name of a field that contains a string. If the string contains a decimal point ( . ), then the tonumber function converts it to a double. Otherwise, the function converts the string to an integer.

Be aware that integers are supported differently in different product contexts:
The argument is optional. It defines the base of the number in the argument. It defaults to 10, which corresponds to the decimal system. You can set to a number between 2 and 36, inclusive.

If the tonumber function cannot parse a literal string to a number, the function returns an error.

Basic examples
The following example converts the string values from the store_sales field to numbers, and then stores the numbers in a field named n. This example uses the default base of 10.
... | eval n=tonumber(store_sales)

The following example takes the hexadecimal number and uses a base of 16 to return the number "164".

... | eval n=tonumber("0A4",16)

Related Issues

Resolves #[4514]

Check List

  • [ x] New functionality includes testing.
  • [x ] New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
… default duration is in seconds which will be used for duration format , added cast call for tostring with 1 argument

Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
@asifabashar
Copy link
Author

@penghuo please review

asifabashar and others added 3 commits October 21, 2025 21:21
Signed-off-by: Asif Bashar <[email protected]>
Co-authored-by: ritvibhatt <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
@asifabashar asifabashar requested a review from penghuo October 22, 2025 04:44
@asifabashar
Copy link
Author

@penghuo please review

Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>

Following example converts a string in binary to the number representation::

os> source=EMP | eval int_value = tonumber('010101',2) | fields int_value | head 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using source=people, align with other test in same doc.

// TEXT FUNCTIONS
SUBSTR: 'SUBSTR';
SUBSTRING: 'SUBSTRING';
TONUMBER: 'TONUMBER';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void testToNumberWithDecimalBase16() {
assertEquals(255.5, ToNumberFunction.toNumber("FF.8", 16));
assertEquals(16.25, ToNumberFunction.toNumber("10.4", 16));
assertEquals(171.6875, ToNumberFunction.toNumber("AB.B", 16));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any standard we are following?
Seems SPL does not support tonumber("AB.B", 16)

If not, we should define the behaviour of tonumber as

  1. if base=10, convert to int / double, depend on whether there is .
  2. if base!=0, always convert to long, e.g. new BigInteger("FF", 16).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants