Преглед на файлове

Numbers can now have their number of decimal points set explicitly.

This lets the volume meters not jitter around, but also lets things that should be fixed precision
tags/v1.1.0
Fen Dweller преди 5 години
родител
ревизия
898c68cc2d
променени са 2 файла, в които са добавени 24 реда и са изтрити 15 реда
  1. +3
    -3
      game.js
  2. +21
    -12
      units.js

+ 3
- 3
game.js Целия файл

@@ -4349,9 +4349,9 @@ function cooldown_end(category) {
});
}

function transformNumbers(line)
function transformNumbers(line, fixed=undefined)
{
return line.toString().replace(/[0-9]+(\.[0-9]+)?(e\+[0-9]+)?/g, function(text) { return number(text, numbers); });
return line.toString().replace(/[0-9]+(\.[0-9]+)?(e\+[0-9]+)?/g, function(text) { return number(text, numbers, fixed); });
}

function update(lines = [], active=true)
@@ -4410,7 +4410,7 @@ function applyPercentage(name, meterPos) {
}

function stylePercentage(name, storage) {
document.getElementById(name).innerHTML = name + ": " + transformNumbers(volume(storage.amount,unit,false));
document.getElementById(name).innerHTML = name + ": " + transformNumbers(volume(storage.amount,unit,false), 2);
let meterPos = 150 - storage.amount / storage.limit * 150;
applyPercentage(name, meterPos);
}


+ 21
- 12
units.js Целия файл

@@ -40,24 +40,33 @@ function numberRough(value,suffix="") {
}
}
}
function number(value, type="full", precision=3) {

function fixedIfDecimal(num, fixed) {
if (fixed === undefined)
return num.toString();
else;
return num.toFixed(fixed);
}

function number(value, type="full", fixed) {
console.log(value)
var val = parseFloat(value);
switch(type) {
case "full":
if (Math.log(value) / Math.log(10) < 10) {
return val.toFixed(2);
return fixedIfDecimal(val, fixed);
}

case "scientific": return val.toExponential(precision).toString();
case "words": return number_words_repeated(val);
case "prefix": return number_prefix(val);
case "scientific": return val.toExponential(3, fixed).toString();
case "words": return number_words_repeated(val, fixed);
case "prefix": return number_prefix(val, fixed);
}
}

function number_words(value) {
var scale = Math.floor(Math.log(value) / Math.log(1000));
if (scale < 0) {
return value.toString();
return fixedIfDecimal(value, fixed);
}
switch(scale) {
case 0: return value.toString();
@@ -85,14 +94,14 @@ function number_words(value) {
}
}

function number_words_repeated(value) {
function number_words_repeated(value, fixed) {
if (value == Infinity)
return "a lot of";
var scale = Math.floor(Math.log(value) / Math.log(1000));
if (scale < 0)
return value.toFixed(2);
return fixedIfDecimal(value, fixed);
switch(scale) {
case 0: return value.toFixed(2);
case 0: return fixedIfDecimal(value, fixed);
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";
@@ -108,12 +117,12 @@ function number_words_repeated(value) {
}
}

function number_prefix(value) {
function number_prefix(value, fixed) {
var scale = Math.floor(Math.log(value) / Math.log(1000));
if (scale < 0)
return value.toFixed(2);
return fixedIfDecimal(value, fixed);
switch(scale) {
case 0: return value.toFixed(2);
case 0: return fixedIfDecimal(value, fixed);
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";


Loading…
Отказ
Запис