From a36f352b087462e23d4341dda38b528fa61c2b00 Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Tue, 1 Dec 2026 11:20:42 +0530 Subject: [PATCH 1/2] Format small floats without scientific notation in XPath string() --- lib/rexml/functions.rb | 10 +++++++++- test/functions/test_base.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb index d8231ac1..d00e5a1e 100644 --- a/lib/rexml/functions.rb +++ b/lib/rexml/functions.rb @@ -161,7 +161,15 @@ def string( object=@context[:node] ) if object == integer "%d" % integer else - object.to_s + str = object.to_s + if str.include?("e-") + sign = str.start_with?("-") ? "-" : "" + mantissa, exp = str.delete_prefix("-").split("e-", 2) + digits = mantissa.delete(".").sub(/0+\z/, "") + "#{sign}0.#{'0' * (exp.to_i - 1)}#{digits}" + else + str + end end end else diff --git a/test/functions/test_base.rb b/test/functions/test_base.rb index 206d2e3b..773b67fb 100644 --- a/test/functions/test_base.rb +++ b/test/functions/test_base.rb @@ -254,6 +254,18 @@ def test_string_infinity ]) end + def test_string_small_float + doc = REXML::Document.new("") + 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))"), + ]) + end + # Submitted by Kou def test_lang d = Document.new(<<-XML) From 757c966f95fe8a7f4d2d0b37fd8343e56fccefd6 Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Tue, 1 Dec 2026 19:10:49 +0530 Subject: [PATCH 2/2] Use one assertion per expression in test_string_small_float --- test/functions/test_base.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/functions/test_base.rb b/test/functions/test_base.rb index 773b67fb..d9759f3e 100644 --- a/test/functions/test_base.rb +++ b/test/functions/test_base.rb @@ -256,14 +256,11 @@ def test_string_infinity def test_string_small_float doc = REXML::Document.new("") - 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))")) end # Submitted by Kou