Repository navigation
Commit f3ba580
authored
Format small floats without scientific notation in XPath string() (#395)
The XPath `string()` function formats a number that is not an integer
with Ruby's `Float#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), and
`number()` cannot read such a string back:
```ruby
doc = REXML::Document.new("<a/>")
REXML::XPath.first(doc, "string(0.00001)") # => "1.0e-05"
REXML::XPath.first(doc, "string(0.0000123)") # => "1.23e-05"
REXML::XPath.first(doc, "string(1 div 10000000000)") # => "1.0e-10"
REXML::XPath.first(doc, "number(string(0.00001))") # => NaN
```
After this change a value whose `to_s` has a negative exponent is
written in decimal form (`"0.00001"`, `"0.0000123"`, `"0.0000000001"`,
and `"-0.00001"` for a negative one), so `number(string(x))` gives `x`
back. Integers, and floats whose `to_s` has no exponent, are unchanged.1 parent 4d3849b commit f3ba580
2 files changed
Lines changed: 18 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
165 | 173 | | |
166 | 174 | | |
167 | 175 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
257 | 266 | | |
258 | 267 | | |
259 | 268 | | |
| |||
0 commit comments