Repository navigation
Format small floats without scientific notation in XPath string() - #395
Merged
naitoh merged 2 commits intoDec 1, 2026
Merged
Conversation
naitoh
reviewed
Dec 1, 2026
Comment on lines
+259
to
+266
| assert_equal(["0.00001", "-0.00001", "0.0000123", "0.0000000001", 0.00001], | ||
| [ | ||
| REXML::XPath.first(doc, "string(0.00001)"), | ||
| REXML::XPath.first(doc, "string(-0.00001)"), | ||
| REXML::XPath.first(doc, "string(0.0000123)"), | ||
| REXML::XPath.first(doc, "string(1 div 10000000000)"), | ||
| REXML::XPath.first(doc, "number(string(0.00001))"), | ||
| ]) |
Contributor
There was a problem hiding this comment.
Using individual assert_equal calls makes it easier to see which expected value corresponds to which XPath expression:
Suggested change
| assert_equal(["0.00001", "-0.00001", "0.0000123", "0.0000000001", 0.00001], | |
| [ | |
| REXML::XPath.first(doc, "string(0.00001)"), | |
| REXML::XPath.first(doc, "string(-0.00001)"), | |
| REXML::XPath.first(doc, "string(0.0000123)"), | |
| REXML::XPath.first(doc, "string(1 div 10000000000)"), | |
| REXML::XPath.first(doc, "number(string(0.00001))"), | |
| ]) | |
| assert_equal("0.00001", REXML::XPath.first(doc, "string(0.00001)")) | |
| assert_equal("-0.00001", REXML::XPath.first(doc, "string(-0.00001)")) | |
| assert_equal("0.0000123", REXML::XPath.first(doc, "string(0.0000123)")) | |
| assert_equal("0.0000000001", REXML::XPath.first(doc, "string(1 div 10000000000)")) | |
| assert_equal(0.00001, REXML::XPath.first(doc, "number(string(0.00001))")) |
Contributor
Author
|
Thanks, applied: |
Contributor
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The XPath
string()function formats a number that is not an integer with Ruby'sFloat#to_s, which switches to scientific notation for small values. XPath 1.0 has no exponent syntax for numbers (section 4.2 describes the number-to-string conversion in decimal form), andnumber()cannot read such a string back:After this change a value whose
to_shas a negative exponent is written in decimal form ("0.00001","0.0000123","0.0000000001", and"-0.00001"for a negative one), sonumber(string(x))givesxback. Integers, and floats whoseto_shas no exponent, are unchanged.Test:
test_string_small_floatintest/functions/test_base.rbfails onmaster(["0.00001", ...]expected but["1.0e-05", ...]found, andNaNfor the round trip) and passes now.bundle exec rake testpasses (872 tests, 0 failures, 0 errors, 5 omissions). Ruby 3.4.5; the repo has no RuboCop configured here.