Skip to content

Commit 02cdcd0

Browse files
author
Philipp Hempel
committed
added section about usage of json.Number to Readme
see #78184 see #78185
1 parent 42c958a commit 02cdcd0

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,40 @@ Expected http status code, if the response has another status code, the test cas
467467
}
468468
```
469469

470+
## Comparison of numerical values
471+
472+
To represent numerical values (integers, floats) are stored `json.Number` (strings). This means that not the numerical value is compared, but the string representation.
473+
474+
This means differences in precision, trailing zeroes or format will cause a comparison to fail, even if the values are mathematically equal. E.g. the following values **are not equal**:
475+
476+
* `1``1.0`
477+
* `2.3``2.30`
478+
* `4``04`
479+
* `5.0``5.00`
480+
* `6e3``6000`
481+
482+
### Explicit number casting
483+
484+
To explicitly compare the numerical values, the `json.Number` representation can be cast into primitive types using [sprig type conversion functions](https://masterminds.github.io/sprig/conversion.html).
485+
486+
E.g., if this expression using a variable (internally a `json.Number`):
487+
488+
```jsonc
489+
{{ if eq $idx 3 }}
490+
491+
{{ end }}
492+
```
493+
494+
causes a parser error like `error calling eq: incompatible types for comparison: json.Number and int64`, then the string representation must be cast:
495+
496+
497+
```jsonc
498+
{{ if eq ( int64 $idx ) 3 }}
499+
500+
{{ end }}
501+
```
502+
503+
470504
## Override template delimiters
471505

472506
Go template delimiters can be redefined as part of a single line comment in any of these syntax:

0 commit comments

Comments
 (0)