Skip to content

Instantly share code, notes, and snippets.

@Aurumaker72
Created March 10, 2023 17:41
Show Gist options
  • Select an option

  • Save Aurumaker72/dead103379b108b89d4a15526719c2f8 to your computer and use it in GitHub Desktop.

Select an option

Save Aurumaker72/dead103379b108b89d4a15526719c2f8 to your computer and use it in GitHub Desktop.
Cookie Clicker Number Formatting for TS
import type { INumberFormatterService } from "../services/INumberFormatterService";
export class BigFormatterService implements INumberFormatterService {
private static formatLong: string[] = [' thousand', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion', ' sextillion', ' septillion', ' octillion', ' nonillion'];
private static prefixes = ['', 'un', 'duo', 'tre', 'quattuor', 'quin', 'sex', 'septen', 'octo', 'novem'];
private static suffixes = ['decillion', 'vigintillion', 'trigintillion', 'quadragintillion', 'quinquagintillion', 'sexagintillion', 'septuagintillion', 'octogintillion', 'nonagintillion'];
constructor() {
for (var i in BigFormatterService.suffixes) {
for (var ii in BigFormatterService.prefixes) {
BigFormatterService.formatLong.push(' ' + BigFormatterService.prefixes[ii] + BigFormatterService.suffixes[i]);
}
}
}
format(value: number): string {
let floats = 0;
var negative = (value < 0);
var decimal = '';
var fixed = value.toFixed(floats);
if (floats > 0 && Math.abs(value) < 1000 && Math.floor(Number(fixed)) != Number(fixed)) decimal = '.' + (fixed.toString()).split('.')[1];
value = Math.floor(Math.abs(value));
if (floats > 0 && fixed == (value + 1).toString()) value++;
var format = 1;
var formatter = function (val: number) {
var base = 0, notationValue = '';
if (!isFinite(val)) return 'Infinity';
if (val >= 1000000) {
val /= 1000;
while (Math.round(val) >= 1000) {
val /= 1000;
base++;
}
if (base >= BigFormatterService.formatLong.length) { return 'Infinity'; } else { notationValue = BigFormatterService.formatLong[base]; }
}
return (Math.round(val * 1000) / 1000) + notationValue;
};
var output = (value.toString().indexOf('e+') != -1 && format == 2) ? value.toPrecision(3).toString() : formatter(value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
if (output == '0') negative = false;
return (negative ? '-' + output : output + decimal);
}
}
export interface INumberFormatterService {
/**
* Formats the specified value into a string
* @param value The number to format
*/
format(value: number): string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment