Cleaned up CM.Disp.Beautify
This commit is contained in:
148
CookieMonster.js
148
CookieMonster.js
@@ -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();
|
||||||
if (CM.Config.Scale == 3) {
|
var timesTenToPowerThree = Math.trunc(Math.log10(num) / 3)
|
||||||
if (num >= 999999) {
|
if (timesTenToPowerThree < 2) {
|
||||||
var count = 0;
|
answer = num;
|
||||||
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 (CM.Config.Scale == 4) {
|
else if (CM.Config.Scale == 3 && !forced || forced == 3) { // Scientific notation, 123456789 => 1.235E+8
|
||||||
if (um >= 999999) {
|
answer = num[0] + '.'
|
||||||
var count = 0;
|
i = 0;
|
||||||
while (num >= 1000) {
|
while (i < decimals - 1) {
|
||||||
count++;
|
answer += num[i + 2]; // num has a 0-based index and [1] is a '.'
|
||||||
num /= 1000;
|
i++;
|
||||||
}
|
|
||||||
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + (count * 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);
|
|
||||||
}
|
}
|
||||||
|
answer += Math.round(num[i + 2] + '.' + num[i + 3]);
|
||||||
|
answer += 'E+' + Math.trunc(Math.log10(num));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (var i = (CM.Disp.shortScale.length - 1); i >= 0; i--) {
|
var restOfNumber = (num / Math.pow(10, (timesTenToPowerThree * 3))).toString();
|
||||||
if (i < CM.Disp.metric.length && CM.Config.Scale == 1) {
|
numbersToAdd = (restOfNumber.indexOf('.') > -1 ? restOfNumber.indexOf('.') + 1 + decimals : (restOfNumber.length))
|
||||||
// Revert to Scientific Notation from e27
|
i = 0
|
||||||
if (Math.log10(num) > 27) {
|
while (i < numbersToAdd - 1) {
|
||||||
if (num >= 999999) {
|
answer += restOfNumber[i];
|
||||||
var count = 0;
|
i++
|
||||||
while (num >= 10) {
|
}
|
||||||
count++;
|
answer += (i + 1 < restOfNumber.length ? Math.round(restOfNumber[i] + '.' + restOfNumber[i + 1]) : restOfNumber[i]);
|
||||||
num /= 10;
|
|
||||||
}
|
// answer is now "xxx.xx" (e.g., 123456789 would be 123.46)
|
||||||
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + count;
|
if (CM.Config.Scale == 1 && !forced || forced == 1) { // Metric scale, 123456789 => 123.457 M
|
||||||
}
|
if (timesTenToPowerThree - 1 < CM.Disp.metric.length) {
|
||||||
else if (num < -999999 && num != 0) {
|
answer += ' ' + CM.Disp.metric[timesTenToPowerThree - 1]
|
||||||
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) {
|
else { // If number is too large, revert to scientific notation
|
||||||
if (num >= Math.pow(1000, i + 2)) {
|
return CM.Disp.Beautify(num, 0, 3);
|
||||||
answer = (Math.round(num / Math.pow(1000, i + 1)) / 1000) + ' ' + CM.Disp.shortScale[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (CM.Config.Scale == 2 && !forced || forced == 2) { // Short scale, 123456789 => 123.457 M
|
||||||
|
if (timesTenToPowerThree < CM.Disp.shortScale.length + 1) {
|
||||||
|
answer += ' ' + CM.Disp.shortScale[timesTenToPowerThree - 1];
|
||||||
|
}
|
||||||
|
else { // If number is too large, revert to scientific notation
|
||||||
|
return CM.Disp.Beautify(num, 0, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
|||||||
151
src/Disp.js
151
src/Disp.js
@@ -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();
|
||||||
if (CM.Config.Scale == 3) {
|
var timesTenToPowerThree = Math.trunc(Math.log10(num) / 3)
|
||||||
if (num >= 999999) {
|
if (timesTenToPowerThree < 2) {
|
||||||
var count = 0;
|
answer = num;
|
||||||
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 (CM.Config.Scale == 4) {
|
else if (CM.Config.Scale == 3 && !forced || forced == 3) { // Scientific notation, 123456789 => 1.235E+8
|
||||||
if (um >= 999999) {
|
answer = num[0] + '.'
|
||||||
var count = 0;
|
i = 0;
|
||||||
while (num >= 1000) {
|
while (i < decimals - 1) {
|
||||||
count++;
|
answer += num[i + 2]; // num has a 0-based index and [1] is a '.'
|
||||||
num /= 1000;
|
i++;
|
||||||
}
|
|
||||||
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + (count * 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);
|
|
||||||
}
|
}
|
||||||
|
answer += Math.round(num[i + 2] + '.' + num[i + 3]);
|
||||||
|
answer += 'E+' + Math.trunc(Math.log10(num));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (var i = (CM.Disp.shortScale.length - 1); i >= 0; i--) {
|
var restOfNumber = (num / Math.pow(10, (timesTenToPowerThree * 3))).toString();
|
||||||
if (i < CM.Disp.metric.length && CM.Config.Scale == 1) {
|
numbersToAdd = (restOfNumber.indexOf('.') > -1 ? restOfNumber.indexOf('.') + 1 + decimals : (restOfNumber.length))
|
||||||
// Revert to Scientific Notation from e27
|
i = 0
|
||||||
if (Math.log10(num) > 27) {
|
while (i < numbersToAdd - 1) {
|
||||||
if (num >= 999999) {
|
answer += restOfNumber[i];
|
||||||
var count = 0;
|
i++
|
||||||
while (num >= 10) {
|
}
|
||||||
count++;
|
answer += (i + 1 < restOfNumber.length ? Math.round(restOfNumber[i] + '.' + restOfNumber[i + 1]) : restOfNumber[i]);
|
||||||
num /= 10;
|
|
||||||
}
|
// answer is now "xxx.xx" (e.g., 123456789 would be 123.46)
|
||||||
answer = +(Math.round(num + "e+2") + "e-2") + 'E+' + count;
|
if (CM.Config.Scale == 1 && !forced || forced == 1) { // Metric scale, 123456789 => 123.457 M
|
||||||
}
|
if (timesTenToPowerThree - 1 < CM.Disp.metric.length) {
|
||||||
else if (num < -999999 && num != 0) {
|
answer += ' ' + CM.Disp.metric[timesTenToPowerThree - 1]
|
||||||
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) {
|
else { // If number is too large, revert to scientific notation
|
||||||
if (num >= Math.pow(1000, i + 2)) {
|
return CM.Disp.Beautify(num, 0, 3);
|
||||||
answer = (Math.round(num / Math.pow(1000, i + 1)) / 1000) + ' ' + CM.Disp.shortScale[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (CM.Config.Scale == 2 && !forced || forced == 2) { // Short scale, 123456789 => 123.457 M
|
||||||
|
if (timesTenToPowerThree < CM.Disp.shortScale.length + 1) {
|
||||||
|
answer += ' ' + CM.Disp.shortScale[timesTenToPowerThree - 1];
|
||||||
|
}
|
||||||
|
else { // If number is too large, revert to scientific notation
|
||||||
|
return CM.Disp.Beautify(num, 0, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user