Skip to content

Commit

Permalink
apacheGH-44273: [C++][Decimal] Use 0E+1 not 0.E+1 for broader compati…
Browse files Browse the repository at this point in the history
…bility

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`.

Use `0E+1` not `0.E+1` for broader compatibility.
  • Loading branch information
kou committed Oct 1, 2024
1 parent 6f64af5 commit bfc672f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 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 @@ -476,7 +476,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 @@ -689,22 +689,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

0 comments on commit bfc672f

Please sign in to comment.