diff options
author | Simon Hausmann <[email protected]> | 2016-08-26 20:39:48 +0200 |
---|---|---|
committer | Simon Hausmann <[email protected]> | 2016-08-26 20:40:28 +0200 |
commit | 830a55ef58a21bf342eef1c2544bcbc899014e7b (patch) | |
tree | e15d5fbe038eb1c39e16b069453785c09705f328 /src/qml/jsruntime/qv4numberobject.cpp | |
parent | 86f81f48c6ceed124dabddf68994dcf3c0488509 (diff) | |
parent | 48dce24717ca537a6f5d817b2f5cd689df86ad68 (diff) |
Merge remote-tracking branch 'origin/5.7' into 5.8
Change-Id: If6750f5a11a24c535a2b3c4923bd8dc6a6b818f2
Diffstat (limited to 'src/qml/jsruntime/qv4numberobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4numberobject.cpp | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/qml/jsruntime/qv4numberobject.cpp b/src/qml/jsruntime/qv4numberobject.cpp index d27eef1d79..f8e0d8763b 100644 --- a/src/qml/jsruntime/qv4numberobject.cpp +++ b/src/qml/jsruntime/qv4numberobject.cpp @@ -285,12 +285,30 @@ ReturnedValue NumberPrototype::method_toPrecision(CallContext *ctx) if (!ctx->argc() || ctx->args()[0].isUndefined()) return RuntimeHelpers::toString(scope.engine, v); - double precision = ctx->args()[0].toInt32(); + int precision = ctx->args()[0].toInt32(); if (precision < 1 || precision > 21) { ScopedString error(scope, scope.engine->newString(QStringLiteral("Number.prototype.toPrecision: precision out of range"))); return ctx->engine()->throwRangeError(error); } - QString result = NumberLocale::instance()->toString(v->asDouble(), 'g', precision); + // TODO: Once we get a NumberOption to retain trailing zeroes, replace the code below with: + // QString result = NumberLocale::instance()->toString(v->asDouble(), 'g', precision); + QByteArray format = "%#." + QByteArray::number(precision) + "g"; + QString result = QString::asprintf(format.constData(), v->asDouble()); + if (result.endsWith(QLatin1Char('.'))) { + // This is 'f' notation, not 'e'. + result.chop(1); + } else { + int ePos = result.indexOf(QLatin1Char('e')); + if (ePos != -1) { + Q_ASSERT(ePos + 2 < result.length()); // always '+' or '-', and number, after 'e' + Q_ASSERT(ePos > 0); // 'e' is not the first character + + if (result.at(ePos + 2) == QLatin1Char('0')) // Drop leading zeroes in exponent + result = result.remove(ePos + 2, 1); + if (result.at(ePos - 1) == QLatin1Char('.')) // Drop trailing dots before 'e' + result = result.remove(ePos - 1, 1); + } + } return scope.engine->newString(result)->asReturnedValue(); } |