Skip to content

Commit

Permalink
GH-44273: [C++][Decimal] Use 0E+1 not 0.E+1 for broader compatibility (
Browse files Browse the repository at this point in the history
…#44275)

### Rationale for this change

Most environments such as Python, Node.js, PostgreSQL and MySQL accepts `0.E+1` but some environments such as Ruby don't accept `0.E+1`. More environments accept `0.0E+1` or `0E+1` than `0.E+1`.

### What changes are included in this PR?

Use `0E+1` not `0.E+1` for broader compatibility.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

Yes.
* GitHub Issue: #44273

Authored-by: Sutou Kouhei <kou@clear-code.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
kou authored Oct 9, 2024
1 parent 38c1286 commit d4516c5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
9 changes: 8 additions & 1 deletion cpp/src/arrow/util/decimal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,14 @@ static void AdjustIntegerStringWithScale(int32_t scale, std::string* str) {
// adjusted_exponent = -7
// After inserting decimal point: *str = "-1.23"
// After appending exponent: *str = "-1.23E-7"
str->insert(str->begin() + 1 + is_negative_offset, '.');
// Example 3:
// Precondition: *str = "0", is_negative_offset = 0, num_digits = 1, scale = -1,
// adjusted_exponent = 1
// After inserting decimal point: *str = "0" // Not inserted
// After appending exponent: *str = "0E+1"
if (num_digits > 1) {
str->insert(str->begin() + 1 + is_negative_offset, '.');
}
str->push_back('E');
if (adjusted_exponent >= 0) {
str->push_back('+');
Expand Down
14 changes: 7 additions & 7 deletions cpp/src/arrow/util/decimal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -711,22 +711,22 @@ struct ToStringTestParam {
};

static const ToStringTestParam kToStringTestData[] = {
{0, -1, "0.E+1"},
{0, -1, "0E+1"},
{0, 0, "0"},
{0, 1, "0.0"},
{0, 6, "0.000000"},
{2, 7, "2.E-7"},
{2, -1, "2.E+1"},
{2, 7, "2E-7"},
{2, -1, "2E+1"},
{2, 0, "2"},
{2, 1, "0.2"},
{2, 6, "0.000002"},
{-2, 7, "-2.E-7"},
{-2, 7, "-2.E-7"},
{-2, -1, "-2.E+1"},
{-2, 7, "-2E-7"},
{-2, 7, "-2E-7"},
{-2, -1, "-2E+1"},
{-2, 0, "-2"},
{-2, 1, "-0.2"},
{-2, 6, "-0.000002"},
{-2, 7, "-2.E-7"},
{-2, 7, "-2E-7"},
{123, -3, "1.23E+5"},
{123, -1, "1.23E+3"},
{123, 1, "12.3"},
Expand Down
14 changes: 7 additions & 7 deletions cpp/src/arrow/util/formatting_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,22 @@ void TestDecimalFormatter() {

// Borrow from Decimal::ToString test
const auto decimalTestData = std::vector<TestParam>{
{0, -1, "0.E+1"},
{0, -1, "0E+1"},
{0, 0, "0"},
{0, 1, "0.0"},
{0, 6, "0.000000"},
{2, 7, "2.E-7"},
{2, -1, "2.E+1"},
{2, 7, "2E-7"},
{2, -1, "2E+1"},
{2, 0, "2"},
{2, 1, "0.2"},
{2, 6, "0.000002"},
{-2, 7, "-2.E-7"},
{-2, 7, "-2.E-7"},
{-2, -1, "-2.E+1"},
{-2, 7, "-2E-7"},
{-2, 7, "-2E-7"},
{-2, -1, "-2E+1"},
{-2, 0, "-2"},
{-2, 1, "-0.2"},
{-2, 6, "-0.000002"},
{-2, 7, "-2.E-7"},
{-2, 7, "-2E-7"},
{123, -3, "1.23E+5"},
{123, -1, "1.23E+3"},
{123, 1, "12.3"},
Expand Down
4 changes: 1 addition & 3 deletions ruby/red-arrow/lib/arrow/decimal128-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
module Arrow
class Decimal128Array
def get_value(i)
string = format_value(i)
string.sub!(".E", ".0E") if string.include?(".E")
BigDecimal(string)
BigDecimal(format_value(i))
end
end
end
4 changes: 1 addition & 3 deletions ruby/red-arrow/lib/arrow/decimal256-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ module Arrow
class Decimal256Array
# @since 3.0.0
def get_value(i)
string = format_value(i)
string.sub!(".E", ".0E") if string.include?(".E")
BigDecimal(string)
BigDecimal(format_value(i))
end
end
end

0 comments on commit d4516c5

Please sign in to comment.