[
https://issues.apache.org/jira/browse/TINKERPOP-3166?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18003930#comment-18003930
]
ASF GitHub Bot commented on TINKERPOP-3166:
-------------------------------------------
andreachild commented on code in PR #3153:
URL: https://github.com/apache/tinkerpop/pull/3153#discussion_r2193648470
##########
docs/src/reference/the-traversal.asciidoc:
##########
@@ -829,6 +829,37 @@ g.inject(datetime("2023-08-24T00:00:00Z")).asDate() <3>
link:++https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#asDate()++[`asDate()`]
+[[asNumber-step]]
+=== AsNumber Step
+
+The `asNumber()`-step (*map*) converts the incoming traverser to the nearest
parsable type if no argument is provided, or to the desired numerical type,
based on the number token (N) provided.
+
+Numerical input will pass through unless a type is specified by the number
token, with the exception that any float number will be converted into double.
`ArithmeticException` will be thrown for any overflow during narrowing of types.
+
+String input will be parsed. By default, the smalled unit of number to be
parsed into is `int` if no number token is provided. `NumberFormatException`
will be thrown for any unparsable strings.
+
+All other input types will result in `IllegalArgumentException`.
+
+[gremlin-groovy,modern]
+----
+g.inject(1).asNumber() <1>
+g.inject(1.76).asNumber() <2>
+g.inject(1.76).asNumber(N.nint) <3>
+g.inject("1b").asNumber() <4>
+g.inject(33550336).asNumber(N.nbyte) <5>
+----
+
+<1> An int will be passed through.
+<2> A double will be passed through.
+<3> A double is converted into an in.
Review Comment:
```suggestion
<3> A double is converted into an int.
```
> Add number conversion step asNumber()
> -------------------------------------
>
> Key: TINKERPOP-3166
> URL: https://issues.apache.org/jira/browse/TINKERPOP-3166
> Project: TinkerPop
> Issue Type: Improvement
> Components: language
> Affects Versions: 3.8.0
> Reporter: Yang Xia
> Priority: Major
>
> Given the addition of the {{asString()}} and {{asDate()}} steps in the 3.7
> line, it should also be helpful to add an {{asNumber()}} step that does
> numerical casting/conversions.
> The current idea is for the {{asNumber()}} step to convert the incoming
> traverser to the nearest parsable type (e.g. int or double) if no argument is
> provided, or to the desired numerical type, based on a number token
> ({{{}N{}}}) provided. Like the {{asDate()}} step, it will not be scoped (for
> now, scopes can be added in the future).
> Some conjured examples:
> {code:java}
> gremlin> g.inject(5).asNumber()
> ==> 5 // parses to int
> gremlin> g.inject(5.123f).asNumber()
> ==> 5.123
> gremlin> g.inject(5.43).asNumber(N.int)
> ==> 5 {code}
> More details can be found in the [proposal
> doc|https://github.com/apache/tinkerpop/blob/master/docs/src/dev/future/proposal-asnumber-step-6.asciidoc].
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)