Cleaned up CM.Disp.Beautify

This commit is contained in:
Daniel van Noord
2020-12-03 10:50:00 +01:00
parent 7efbd59062
commit 391b70017a
2 changed files with 129 additions and 174 deletions

View File

@@ -1165,9 +1165,9 @@ CM.Disp.RefreshScale = function() {
/** /**
* This function returns time as a string depending on TimeFormat setting * This function returns time as a string depending on TimeFormat setting
* @param {number} time Time as number * @param {number} time Time to be formatted
* @param {number} longFormat 1 or 0 * @param {number} longFormat 1 or 0
* @return {string} * @return {string} Formatted time`
*/ */
CM.Disp.FormatTime = function(time, longFormat) { CM.Disp.FormatTime = function(time, longFormat) {
if (time == Infinity) return time; if (time == Infinity) return time;
@@ -1195,105 +1195,83 @@ CM.Disp.FormatTime = function(time, longFormat) {
return str; return str;
} }
CM.Disp.Beautify = function(num, frac) { /**
if (CM.Config.Scale != 0 && isFinite(num)) { * This function returns formats number based on the Scale setting
* @param {number} num Number to be beautified
* @param {any} frac Used in some scenario's by CM.Backup.Beautify (Game's original function)
* @param {number} forced Used to force (type 3) in certains cases
* @return {string} Formatted number
* TODO: Add functionality to choose amount of decimals and separators
*/
CM.Disp.Beautify = function(num, frac, forced) {
var decimals = 3; // This can be used to implement function to let user choose amount of decimals
if (CM.Config.Scale == 0) {
return CM.Backup.Beautify(num, frac);
}
else if (isFinite(num)) {
var answer = ''; var answer = '';
var negative = false;
if (num < 0) { if (num < 0) {
num = Math.abs(num); num = Math.abs(num);
negative = true; var negative = true;
} }
num = num.toString();
var timesTenToPowerThree = Math.trunc(Math.log10(num) / 3)
if (timesTenToPowerThree < 2) {
answer = num;
}
else if (CM.Config.Scale == 3 && !forced || forced == 3) { // Scientific notation, 123456789 => 1.235E+8
answer = num[0] + '.'
i = 0;
while (i < decimals - 1) {
answer += num[i + 2]; // num has a 0-based index and [1] is a '.'
i++;
}
answer += Math.round(num[i + 2] + '.' + num[i + 3]);
answer += 'E+' + Math.trunc(Math.log10(num));
}
else {
var restOfNumber = (num / Math.pow(10, (timesTenToPowerThree * 3))).toString();
numbersToAdd = (restOfNumber.indexOf('.') > -1 ? restOfNumber.indexOf('.') + 1 + decimals : (restOfNumber.length))
i = 0
while (i < numbersToAdd - 1) {
answer += restOfNumber[i];
i++
}
answer += (i + 1 < restOfNumber.length ? Math.round(restOfNumber[i] + '.' + restOfNumber[i + 1]) : restOfNumber[i]);
if (CM.Config.Scale == 3) { // answer is now "xxx.xx" (e.g., 123456789 would be 123.46)
if (num >= 999999) { if (CM.Config.Scale == 1 && !forced || forced == 1) { // Metric scale, 123456789 => 123.457 M
var count = 0; if (timesTenToPowerThree - 1 < CM.Disp.metric.length) {
while (num >= 10) { answer += ' ' + CM.Disp.metric[timesTenToPowerThree - 1]
count++;
num /= 10;
} }
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + count; else { // If number is too large, revert to scientific notation
} return CM.Disp.Beautify(num, 0, 3);
else if (num < -999999 && num != 0) {
var count = 0;
while (num < 1) {
count++;
num *= 10;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E-' + count;
}
else {
answer = CM.Backup.Beautify(num, frac);
} }
} }
else if (CM.Config.Scale == 4) { else if (CM.Config.Scale == 2 && !forced || forced == 2) { // Short scale, 123456789 => 123.457 M
if (um >= 999999) { if (timesTenToPowerThree < CM.Disp.shortScale.length + 1) {
var count = 0; answer += ' ' + CM.Disp.shortScale[timesTenToPowerThree - 1];
while (num >= 1000) {
count++;
num /= 1000;
} }
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + (count * 3); else { // If number is too large, revert to scientific notation
} return CM.Disp.Beautify(num, 0, 3);
else if (num < -999999 && num != 0) {
var count = 0;
while (num < 1) {
count++;
num *= 1000;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E-' + (count * 3);
}
else {
answer = CM.Backup.Beautify(num, frac);
}
}
else {
for (var i = (CM.Disp.shortScale.length - 1); i >= 0; i--) {
if (i < CM.Disp.metric.length && CM.Config.Scale == 1) {
// Revert to Scientific Notation from e27
if (Math.log10(num) > 27) {
if (num >= 999999) {
var count = 0;
while (num >= 10) {
count++;
num /= 10;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + count;
}
else if (num < -999999 && num != 0) {
var count = 0;
while (num < 1) {
count++;
num *= 10;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E-' + count;
}
else {
answer = CM.Backup.Beautify(num, frac);
}
}
else if (num >= Math.pow(1000, i + 2)) {
answer = (Math.round(num / Math.pow(1000, i + 1)) / 1000).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' ' + CM.Disp.metric[i];
break;
}
}
else if (CM.Config.Scale == 2) {
if (num >= Math.pow(1000, i + 2)) {
answer = (Math.round(num / Math.pow(1000, i + 1)) / 1000) + ' ' + CM.Disp.shortScale[i];
break;
} }
} }
else if (CM.Config.Scale == 4 && !forced || forced == 4) { // Engineering notation, 123456789 => 123.457E+6
answer += 'E+' + (timesTenToPowerThree * 3);
} }
} }
if (answer == '') { if (answer == '') {
console.log("Could not beautify number with CM.Disp.Beautify");
answer = CM.Backup.Beautify(num, frac); answer = CM.Backup.Beautify(num, frac);
} }
if (negative) answer = '-' + answer;
if (negative) {
answer = '-' + answer;
}
return answer; return answer;
} }
else if (num == Infinity) {
return "Infinity";
}
else { else {
console.log("Could not beautify number with CM.Disp.Beautify");
return CM.Backup.Beautify(num, frac); return CM.Backup.Beautify(num, frac);
} }
} }
@@ -3516,8 +3494,8 @@ CM.Disp.lastAscendState = -1;
CM.Disp.cookieTimes = [10, 15, 30, 60, 300, 600, 900, 1800]; CM.Disp.cookieTimes = [10, 15, 30, 60, 300, 600, 900, 1800];
CM.Disp.clickTimes = [1, 5, 10, 15, 30]; CM.Disp.clickTimes = [1, 5, 10, 15, 30];
CM.Disp.metric = ['M', 'G', 'T', 'P', 'E', 'Z', 'Y']; CM.Disp.metric = ['', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
CM.Disp.shortScale = ['M', 'B', 'Tr', 'Quadr', 'Quint', 'Sext', 'Sept', 'Oct', 'Non', 'Dec', 'Undec', 'Duodec', 'Tredec', 'Quattuordec', 'Quindec', 'Sexdec', 'Septendec', 'Octodec', 'Novemdec', 'Vigint', 'Unvigint', 'Duovigint', 'Trevigint', 'Quattuorvigint']; CM.Disp.shortScale = ['', 'M', 'B', 'Tr', 'Quadr', 'Quint', 'Sext', 'Sept', 'Oct', 'Non', 'Dec', 'Undec', 'Duodec', 'Tredec', 'Quattuordec', 'Quindec', 'Sexdec', 'Septendec', 'Octodec', 'Novemdec', 'Vigint', 'Unvigint', 'Duovigint', 'Trevigint', 'Quattuorvigint'];
CM.Disp.TooltipWrinklerArea = 0; CM.Disp.TooltipWrinklerArea = 0;
CM.Disp.TooltipWrinkler = -1; CM.Disp.TooltipWrinkler = -1;

View File

@@ -318,14 +318,13 @@ CM.Disp.RefreshScale = function() {
CM.Disp.UpdateUpgrades(); CM.Disp.UpdateUpgrades();
} }
/******** /********
* Section: General functions to format or beautify strings * Section: General functions to format or beautify strings */
* TODO: Annotate most functions */
/** /**
* This function returns time as a string depending on TimeFormat setting * This function returns time as a string depending on TimeFormat setting
* @param {number} time Time as number * @param {number} time Time to be formatted
* @param {number} longFormat 1 or 0 * @param {number} longFormat 1 or 0
* @return {string} * @return {string} Formatted time`
*/ */
CM.Disp.FormatTime = function(time, longFormat) { CM.Disp.FormatTime = function(time, longFormat) {
if (time == Infinity) return time; if (time == Infinity) return time;
@@ -353,105 +352,83 @@ CM.Disp.FormatTime = function(time, longFormat) {
return str; return str;
} }
CM.Disp.Beautify = function(num, frac) { /**
if (CM.Config.Scale != 0 && isFinite(num)) { * This function returns formats number based on the Scale setting
* @param {number} num Number to be beautified
* @param {any} frac Used in some scenario's by CM.Backup.Beautify (Game's original function)
* @param {number} forced Used to force (type 3) in certains cases
* @return {string} Formatted number
* TODO: Add functionality to choose amount of decimals and separators
*/
CM.Disp.Beautify = function(num, frac, forced) {
var decimals = 3; // This can be used to implement function to let user choose amount of decimals
if (CM.Config.Scale == 0) {
return CM.Backup.Beautify(num, frac);
}
else if (isFinite(num)) {
var answer = ''; var answer = '';
var negative = false;
if (num < 0) { if (num < 0) {
num = Math.abs(num); num = Math.abs(num);
negative = true; var negative = true;
} }
num = num.toString();
var timesTenToPowerThree = Math.trunc(Math.log10(num) / 3)
if (timesTenToPowerThree < 2) {
answer = num;
}
else if (CM.Config.Scale == 3 && !forced || forced == 3) { // Scientific notation, 123456789 => 1.235E+8
answer = num[0] + '.'
i = 0;
while (i < decimals - 1) {
answer += num[i + 2]; // num has a 0-based index and [1] is a '.'
i++;
}
answer += Math.round(num[i + 2] + '.' + num[i + 3]);
answer += 'E+' + Math.trunc(Math.log10(num));
}
else {
var restOfNumber = (num / Math.pow(10, (timesTenToPowerThree * 3))).toString();
numbersToAdd = (restOfNumber.indexOf('.') > -1 ? restOfNumber.indexOf('.') + 1 + decimals : (restOfNumber.length))
i = 0
while (i < numbersToAdd - 1) {
answer += restOfNumber[i];
i++
}
answer += (i + 1 < restOfNumber.length ? Math.round(restOfNumber[i] + '.' + restOfNumber[i + 1]) : restOfNumber[i]);
if (CM.Config.Scale == 3) { // answer is now "xxx.xx" (e.g., 123456789 would be 123.46)
if (num >= 999999) { if (CM.Config.Scale == 1 && !forced || forced == 1) { // Metric scale, 123456789 => 123.457 M
var count = 0; if (timesTenToPowerThree - 1 < CM.Disp.metric.length) {
while (num >= 10) { answer += ' ' + CM.Disp.metric[timesTenToPowerThree - 1]
count++;
num /= 10;
} }
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + count; else { // If number is too large, revert to scientific notation
} return CM.Disp.Beautify(num, 0, 3);
else if (num < -999999 && num != 0) {
var count = 0;
while (num < 1) {
count++;
num *= 10;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E-' + count;
}
else {
answer = CM.Backup.Beautify(num, frac);
} }
} }
else if (CM.Config.Scale == 4) { else if (CM.Config.Scale == 2 && !forced || forced == 2) { // Short scale, 123456789 => 123.457 M
if (um >= 999999) { if (timesTenToPowerThree < CM.Disp.shortScale.length + 1) {
var count = 0; answer += ' ' + CM.Disp.shortScale[timesTenToPowerThree - 1];
while (num >= 1000) {
count++;
num /= 1000;
} }
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + (count * 3); else { // If number is too large, revert to scientific notation
} return CM.Disp.Beautify(num, 0, 3);
else if (num < -999999 && num != 0) {
var count = 0;
while (num < 1) {
count++;
num *= 1000;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E-' + (count * 3);
}
else {
answer = CM.Backup.Beautify(num, frac);
}
}
else {
for (var i = (CM.Disp.shortScale.length - 1); i >= 0; i--) {
if (i < CM.Disp.metric.length && CM.Config.Scale == 1) {
// Revert to Scientific Notation from e27
if (Math.log10(num) > 27) {
if (num >= 999999) {
var count = 0;
while (num >= 10) {
count++;
num /= 10;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + count;
}
else if (num < -999999 && num != 0) {
var count = 0;
while (num < 1) {
count++;
num *= 10;
}
answer = +(Math.round(num + "e+2") + "e-2") + 'E-' + count;
}
else {
answer = CM.Backup.Beautify(num, frac);
}
}
else if (num >= Math.pow(1000, i + 2)) {
answer = (Math.round(num / Math.pow(1000, i + 1)) / 1000).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' ' + CM.Disp.metric[i];
break;
}
}
else if (CM.Config.Scale == 2) {
if (num >= Math.pow(1000, i + 2)) {
answer = (Math.round(num / Math.pow(1000, i + 1)) / 1000) + ' ' + CM.Disp.shortScale[i];
break;
} }
} }
else if (CM.Config.Scale == 4 && !forced || forced == 4) { // Engineering notation, 123456789 => 123.457E+6
answer += 'E+' + (timesTenToPowerThree * 3);
} }
} }
if (answer == '') { if (answer == '') {
console.log("Could not beautify number with CM.Disp.Beautify");
answer = CM.Backup.Beautify(num, frac); answer = CM.Backup.Beautify(num, frac);
} }
if (negative) answer = '-' + answer;
if (negative) {
answer = '-' + answer;
}
return answer; return answer;
} }
else if (num == Infinity) {
return "Infinity";
}
else { else {
console.log("Could not beautify number with CM.Disp.Beautify");
return CM.Backup.Beautify(num, frac); return CM.Backup.Beautify(num, frac);
} }
} }
@@ -2674,8 +2651,8 @@ CM.Disp.lastAscendState = -1;
CM.Disp.cookieTimes = [10, 15, 30, 60, 300, 600, 900, 1800]; CM.Disp.cookieTimes = [10, 15, 30, 60, 300, 600, 900, 1800];
CM.Disp.clickTimes = [1, 5, 10, 15, 30]; CM.Disp.clickTimes = [1, 5, 10, 15, 30];
CM.Disp.metric = ['M', 'G', 'T', 'P', 'E', 'Z', 'Y']; CM.Disp.metric = ['', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
CM.Disp.shortScale = ['M', 'B', 'Tr', 'Quadr', 'Quint', 'Sext', 'Sept', 'Oct', 'Non', 'Dec', 'Undec', 'Duodec', 'Tredec', 'Quattuordec', 'Quindec', 'Sexdec', 'Septendec', 'Octodec', 'Novemdec', 'Vigint', 'Unvigint', 'Duovigint', 'Trevigint', 'Quattuorvigint']; CM.Disp.shortScale = ['', 'M', 'B', 'Tr', 'Quadr', 'Quint', 'Sext', 'Sept', 'Oct', 'Non', 'Dec', 'Undec', 'Duodec', 'Tredec', 'Quattuordec', 'Quindec', 'Sexdec', 'Septendec', 'Octodec', 'Novemdec', 'Vigint', 'Unvigint', 'Duovigint', 'Trevigint', 'Quattuorvigint'];
CM.Disp.TooltipWrinklerArea = 0; CM.Disp.TooltipWrinklerArea = 0;
CM.Disp.TooltipWrinkler = -1; CM.Disp.TooltipWrinkler = -1;