MCPcopy Create free account
hub / github.com/candybox2/candybox2.github.io / numberToStringButNicely

Function numberToStringButNicely

code/main/Algo.ts:44–61  ·  view source on GitHub ↗
(n: number)

Source from the content-addressed store, hash-verified

42
43 // Transform a number in a string and add whitespaces every three figures
44 export function numberToStringButNicely(n: number): string{
45 // We store the number as a string in the variable str
46 var str: string = n.toString();
47
48 // If the number isn't going to loose precision because it's too big, we add whitespaces every three characters
49 if(n < getMaximumJavascriptInt()){
50 // If the var is bigger than 3 characters
51 if(str.length > 3){
52 for(var i = Math.floor(str.length/3); i > 0; i--){
53 if(i*3 != str.length) // To avoid adding a whitespace at the left of multiple-of-three strings
54 str = str.addAt(str.length-i*3, " ");
55 }
56 }
57 }
58
59 // Return str
60 return str;
61 }
62
63 // Create a string from a number and a word added depending on the plurality of the number
64 export function pluralFormat(n: number, singular: string, plural: string): string{

Callers 1

pluralFormatNicelyFunction · 0.85

Calls 2

getMaximumJavascriptIntFunction · 0.85
addAtMethod · 0.80

Tested by

no test coverage detected