From 207b256cb72bbb9b6d8bdfa21e9fa8c15e6e029c Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Tue, 20 Feb 2018 14:14:46 -0500 Subject: [PATCH] Numbers can now be rendered in full, with prefixes, as words, or as scientific notation --- game.js | 19 ++++++++++++++- stroll.html | 1 + units.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/game.js b/game.js index b9e0e1c..3c36e03 100644 --- a/game.js +++ b/game.js @@ -5,6 +5,9 @@ var maxBowelsDigest = 10; var unit = "metric"; +var numbers = "full"; + + var verbose = true; var biome = "suburb"; @@ -406,6 +409,19 @@ function toggle_units() update(); } +function toggle_numbers() { + switch(numbers) { + case "full": numbers="prefix"; break; + case "prefix": numbers="words"; break; + case "words": numbers = "scientific"; break; + case "scientific": numbers = "full"; break; + } + + document.getElementById("button-numbers").innerHTML = "Numbers: " + numbers.charAt(0).toUpperCase() + numbers.slice(1); + + update(); +} + function toggle_verbose() { verbose = !verbose; @@ -932,7 +948,7 @@ function update(lines = []) lines.forEach(function (x) { var line = document.createElement('div'); - line.innerHTML = x; + line.innerHTML = x.replace(/[0-9]+(\.[0-9]+)?(e\+[0-9]+)?/g, function(text) { return number(text, numbers); }); log.appendChild(line); }); @@ -1149,6 +1165,7 @@ window.addEventListener('load', function(event) { document.getElementById("button-anal_vore").addEventListener("click",anal_vore); document.getElementById("button-stroll").addEventListener("click",toggle_auto); document.getElementById("button-location").addEventListener("click",change_location); + document.getElementById("button-numbers").addEventListener("click",toggle_numbers); document.getElementById("button-units").addEventListener("click",toggle_units); document.getElementById("button-verbose").addEventListener("click",toggle_verbose); document.getElementById("button-grow-lots").addEventListener("click",grow_lots); diff --git a/stroll.html b/stroll.html index d7a003c..71bd45f 100644 --- a/stroll.html +++ b/stroll.html @@ -44,6 +44,7 @@

+ diff --git a/units.js b/units.js index eb71bf3..8707c5b 100644 --- a/units.js +++ b/units.js @@ -2,6 +2,75 @@ function round(number,precision=3) { return Math.round(number*Math.pow(10,precision)) / Math.pow(10,precision); } +function number(value, type="full", precision=3) { + var val = parseFloat(value); + switch(type) { + case "full": return val.toString(); + case "scientific": return val.toExponential(precision).toString(); + case "words": return number_words_repeated(val); + case "prefix": return number_prefix(val); + } +} + +function number_words(value) { + var scale = Math.floor(Math.log(value) / Math.log(1000)); + switch(scale) { + case 0: return value.toString(); + case 1: return Math.round(value / 1e3).toString() + " thousand"; + case 2: return Math.round(value / 1e6).toString() + " million"; + case 3: return Math.round(value / 1e9).toString() + " billion"; + case 4: return Math.round(value / 1e12).toString() + " trillion"; + case 5: return Math.round(value / 1e15).toString() + " quadrillion"; + case 6: return Math.round(value / 1e18).toString() + " quintillion"; + case 7: return Math.round(value / 1e21).toString() + " sextillion"; + case 8: return Math.round(value / 1e24).toString() + " septillion"; + case 9: return Math.round(value / 1e27).toString() + " octillion"; + case 10: return Math.round(value / 1e30).toString() + " nonillion"; + case 11: return Math.round(value / 1e33).toString() + " decillion"; + case 12: return Math.round(value / 1e36).toString() + " undecillion"; + case 13: return Math.round(value / 1e39).toString() + " duodecillion"; + case 14: return Math.round(value / 1e42).toString() + " tredecillion"; + case 15: return Math.round(value / 1e45).toString() + " quattuordecillion"; + case 16: return Math.round(value / 1e48).toString() + " quindecillion"; + case 17: return Math.round(value / 1e51).toString() + " sexdecillion"; + case 18: return Math.round(value / 1e54).toString() + " septendecillion"; + case 19: return Math.round(value / 1e57).toString() + " octodecillion"; + case 20: return Math.round(value / 1e60).toString() + " novemdecillion"; + default: return Math.round(value / 1e63).toString() + " vigintillion"; + } +} + +function number_words_repeated(value) { + var scale = Math.floor(Math.log(value) / Math.log(1000)); + if (value == Infinity) + return "a lot of"; + switch(scale) { + case 0: return value.toString(); + case 1: return Math.round(value / 1e3).toString() + " thousand"; + case 2: return Math.round(value / 1e6).toString() + " million"; + case 3: return Math.round(value / 1e9).toString() + " billion"; + case 4: return Math.round(value / 1e12).toString() + " trillion"; + case 5: return Math.round(value / 1e15).toString() + " quadrillion"; + case 6: return Math.round(value / 1e18).toString() + " quintillion"; + default: return number_words_repeated(value / 1e18) + " quintillion"; + } +} + +function number_prefix(value) { + var scale = Math.floor(Math.log(value) / Math.log(1000)); + switch(scale) { + case 0: return value.toString(); + case 1: return Math.round(value / 1e3).toString() + "K"; + case 2: return Math.round(value / 1e6).toString() + "M"; + case 3: return Math.round(value / 1e9).toString() + "G"; + case 4: return Math.round(value / 1e12).toString() + "T"; + case 5: return Math.round(value / 1e15).toString() + "P"; + case 6: return Math.round(value / 1e18).toString() + "E"; + case 7: return Math.round(value / 1e21).toString() + "Z"; + default: return Math.round(value / 1e24).toString() + "Y"; + } +} + function mass(kg, type="metric", singular=false) { switch(type) { case "metric": return metricMass(kg, singular); break;