Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/content/manual/dev/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1469,13 +1469,14 @@ sections:
body: |

The `tonumber` function parses its input as a number. It
will convert correctly-formatted strings to their numeric
equivalent, leave numbers alone, and give an error on all other input.
will convert booleans and correctly-formatted strings to
their numeric equivalent, leave numbers alone, and give
an error on all other input.

examples:
- program: '.[] | tonumber'
input: '[1, "1"]'
output: ['1', '1']
input: '[1, "1", true]'
output: ['1', '1', '1']

- title: "`toboolean`"
body: |
Expand Down
9 changes: 5 additions & 4 deletions docs/content/manual/v1.8/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1469,13 +1469,14 @@ sections:
body: |

The `tonumber` function parses its input as a number. It
will convert correctly-formatted strings to their numeric
equivalent, leave numbers alone, and give an error on all other input.
will convert booleans and correctly-formatted strings to
their numeric equivalent, leave numbers alone, and give
an error on all other input.

examples:
- program: '.[] | tonumber'
input: '[1, "1"]'
output: ['1', '1']
input: '[1, "1", true]'
output: ['1', '1', '1']

- title: "`toboolean`"
body: |
Expand Down
10 changes: 5 additions & 5 deletions jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,17 @@ static jv f_tonumber(jq_state *jq, jv input) {
jv_free(input);
return number;
}
// Handle conversion to bools here. TODO
if (jv_get_kind(input) == JV_KIND_TRUE) {
jv number = jv_number(1);
jv_free(input);
return number;
}
if (jv_get_kind(input) == JV_KIND_FALSE) {
jv number = jv_number(0);
jv_free(input);
return number;
}
return type_error(input, "cannot be parsed as a number");
}

Expand Down
3 changes: 2 additions & 1 deletion tests/man.test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading