Commit a5108686 authored by David Edmundson's avatar David Edmundson
Browse files

[applets/calculator] Use Qt's in-built number formatting

Summary:
Gets rid of a load of code.
Correctly handles floating point issues.
Results are correct.

Potentially this inadvertedly introduces a behaviour change
of including group separators depending on locale. This could be
considered a good thing, but if it's problematic we can always add a
.replace() at the end of this line.

BUG: 410988

Test Plan:
62 * 1.3 = 80.6

100000000 * 5000000 = 5e+17
(with whatever the right number of zeroes is)

42 * 1.33333 = 6.6666665

Reviewers: #plasma

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D26676
parent 3984da31
......@@ -198,10 +198,6 @@ QQC2.Control {
Math.floor(Math.log(Math.abs(number))/Math.log(10)) + 1;
}
function localizeNumber(number) {
return number.toString().replace(".", Qt.locale().decimalPoint);
}
// use the clipboard datasource for being able to inspect the clipboard content before pasting it
PlasmaCore.DataSource {
id: clipboardSource
......@@ -242,27 +238,15 @@ QQC2.Control {
}
function displayNumber(number) {
display.text = localizeNumber(number);
if (display.length > maxInputLength) {
display.text = localizeNumber(convertToScientific(number, 14));
}
display.text = number.toLocaleString(Qt.locale(), "g", 14);
var decimalsToShow = 9;
// Decrease precision until the text fits to the display.
while (display.contentWidth > display.width && decimalsToShow > 0) {
display.text = localizeNumber(convertToScientific(number, decimalsToShow--));
display.text = number.toLocaleString(Qt.locale(), "g", decimalsToShow--);
}
}
function convertToScientific(number, decimalsToShow){
var exponent = algarismCount(number) - 1;
var roundedNumber = +(number / Math.pow(10, exponent)).toFixed(decimalsToShow);
var scientificFormat = roundedNumber.toString() +
"e" + exponent;
return scientificFormat;
}
Connections {
target: plasmoid;
onPopupEvent: {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment