1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
| namespace PolyLabs
{
/// <summary>
/// Parse a numeric value into a short scale string representation.
/// </summary>
public static class ShortScale
{
/*
* Current Version : 1.3 (Sept 14, 2020)
*/
/*
* The ShortScaleString script parses either a double, float, or int value
* value type into a form that is easier to read. The list supports values up to
* the largest currently accepted value(one centillion). values given larger than
* 999 centillion will be represented as "1000 centillion" and so on.
* It is important to be aware of the limitations of each data type. for intParse, numbers higher than 1 billion
* will throw an overflow error. similarly a float value will also overflow after too large a number is reached.
* for applicatins where the needed use is well over 1+E100 use parseDouble as its maximun output value tested is
* 1000 centillion, thats 306 0's!
*/
/// <summary>
/// The list containing all short scale values.
/// </summary>
public static readonly string[] ShortScaleReference = {"thousand","million","billion","trillion","quadrillion","quintillion",
"sextillion","septillion","octillion","nonillion","decillion",
"undecillion","duodecillion","tredecillion","quattuordecillion","quindecillion",
"sexdecillion","septendecillion","octodecillion","novemdecillion","vigintillion",
"unvigintillion", "duovigintillion","trevigintillion","quattuorvigintillion",
"quinvigitillion","sexvigintillion","septenvigitillion","octovigintillion","novemvigitillion",
"trigintillion","untrigintillion","duotrigintillion","tretrigintillion","quattuortrigintillion",
"quintrigintillion","sextrigintillion","septentrigintillion","octotrigintillion",
"novemtrigintillion","quadragintillion","unquadragintillion","duoquadragintillion",
"trequadragintillion","quattuorquadragintillion","quinquadragintillion","sexquadragintillion",
"septenquadragintillion","octoquadragintillion","novemquadragintillion","quinquagintillion","unquinquagintillion",
"duoquinquagintillion","trequinquagintillion","quattuorquinquagintillion","quinquinquagintillion",
"sexquinquagintillion","septenquinquagintillion","octoquinquagintillion","novemquinquagintillion","sexagintillion",
"unsexagintillion","duosexagintillion","tresexagintillion","quattuorsexagintillion","quinsexagintillion",
"sexsexagintillion","septensexagintillion","octosexagintillion","novemsexagintillion","septuagintillion",
"unseptuagintillion","duoseptuagintillion","treseptuagintillion","quattuorseptuagintillion","quinseptuagintillion",
"sexseptuagintillion","septenseptuagintillion","octoseptuagintillion","novemseptuagintillion",
"octogintillion","unoctogintillion","duooctogintillion","treoctogintillion","quattuoroctogintillion",
"quinoctogintillion","sexoctogintillion","septenoctogintillion","octooctogintillion","novemoctogintillion","nonagintillion",
"unnonagintillion","duononagintillion","trenonagintillion","quattuornonagintillion","quinnonagintillion",
"sexnonagintillion","septennonagintillion","octononagintillion","novemnonagintillion","centillion"};
///<summary>
/// List containing short scale values in symbol form. Can be further expanded in future updates.
/// </summary>
public static readonly string[] ShortScaleSymbolReference = { "K", "M", "B", "T", "q", "Q", "s", "S", "O", "N", "D" };
/// <summary>
/// Parses the double value into short scale notation.
/// </summary>
/// <returns>The short scale string.</returns>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static string ParseDouble(double value, in int precision = 3, double startShortScale = 1000000, bool useSymbol = false)
{
string symbol = "";
string strVal = "";
ParseDoubleInternal(value, ref strVal, ref symbol, precision, startShortScale, useSymbol);
return strVal + " " + symbol;
}
/// <summary>
/// Parses the double value into short scale notation, splitting the data into the separate parts.
/// </summary>
/// <returns>A ShortScaleData object containing the value and symbol.</returns>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static ShortScaleData ParseDoubleSplit(double value, in int precision = 3, in double startShortScale = 1000000, in bool useSymbol = false)
{
ShortScaleData data = new ShortScaleData();
ParseDoubleInternal(value, ref data.value, ref data.symbol, precision, startShortScale, useSymbol);
return data;
}
/// <summary>
/// Parses a double value into short scale notation, splitting the data into the separate parts.
/// </summary>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="data">A referencce to an existing data to reuse the same object.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static void ParseDoubleSplit(double value, ref ShortScaleData data, in int precision = 3, in double startShortScale = 1000000, in bool useSymbol = false)
{
ParseDoubleInternal(value, ref data.value, ref data.symbol, precision, startShortScale, useSymbol);
}
/// <summary>
/// Handles the internal calculation of the short scale string for double values.
/// </summary>
private static void ParseDoubleInternal(double value, ref string strVal, ref string symbol, in int precision, double startShortScale, bool useSymbol)
{
int index = -1;
int isNegative = 1;
string addPrecision = new string ('#', precision);
double precisionValue = Mathf.Pow (10, precision);
if (value < 0)
{
isNegative = -1;
value *= isNegative;
}
else if (!(value > 0d))
{
strVal = "0";
symbol = "";
return;
}
if (value < 1000d || value < startShortScale)
{
strVal = (Math.Floor(value * isNegative * precisionValue) / precisionValue).ToString ("#,#." + addPrecision);
symbol = "";
return;
}
int maxIndex = useSymbol
? ShortScaleSymbolReference.Length - 1
: ShortScaleReference.Length - 1;
while (value >= 1000d && index < maxIndex)
{
value *= 0.001d;
index++;
}
symbol = useSymbol
? ShortScaleSymbolReference[index]
: ShortScaleReference [index];
strVal = (Math.Floor(value * isNegative * precisionValue) / precisionValue)
.ToString("#,#." + addPrecision);
}
/// <summary>
/// Parses the float value into short scale notation.
/// </summary>
/// <returns>The short scale string.</returns>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optional) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion.
/// </param>
public static string ParseFloat(float value, in int precision = 3, in float startShortScale = 1000000, in bool useSymbol = false)
{
string symbol = "";
string strVal = "";
ParseFloatInternal(value, ref strVal, ref symbol, precision, startShortScale, useSymbol);
return strVal + " " + symbol;
}
/// <summary>
/// Parses the float value into short scale notation, splitting the data into the separate parts.
/// </summary>
/// <returns>A ShortScaleData object containing the value and symbol.</returns>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static ShortScaleData ParseFloatSplit(float value, in int precision = 3, in float startShortScale = 1000000, in bool useSymbol = false)
{
ShortScaleData data = new ShortScaleData();
ParseFloatInternal(value, ref data.value, ref data.symbol, precision, startShortScale, useSymbol);
return data;
}
/// <summary>
/// Parses a float value into short scale notation, splitting the data into the separate parts.
/// </summary>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="data">A referencce to an existing data to reuse the same object.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static void ParseFloatSplit(float value, ref ShortScaleData data, in int precision = 3, in float startShortScale = 1000000, in bool useSymbol = false)
{
ParseFloatInternal(value, ref data.value, ref data.symbol, precision, startShortScale, useSymbol);
}
/// <summary>
/// Handles the internal calculation of the short scale string for float values.
/// </summary>
private static void ParseFloatInternal(float value, ref string strVal, ref string symbol, int precision = 3, float startShortScale = 1000000, bool useSymbol = false)
{
int index = -1;
int isNegative = 1;
string addPrecision = new string ('#', precision);
double precisionValue = Mathf.Pow (10, precision);
if (value < 0)
{
isNegative = -1;
value *= isNegative;
}
else if (!(value > 0))
{
strVal = "0";
symbol = "";
return;
}
if (value < 1000 || value < startShortScale)
{
strVal = (Math.Floor(value * isNegative * precisionValue) / precisionValue).ToString ("#,#." + addPrecision);
symbol = "";
return;
}
int maxIndex = useSymbol
? ShortScaleSymbolReference.Length - 1
: ShortScaleReference.Length - 1;
while (value >= 1000.0f && index < maxIndex)
{
value *= 0.001f;
index++;
}
strVal = (Math.Floor(value * isNegative * precisionValue ) / precisionValue ).ToString ("#,#." + addPrecision);
symbol = useSymbol
? ShortScaleSymbolReference[index]
: ShortScaleReference[index];
}
/// <summary>
/// Parses the int value into short scale notation.
/// </summary>
/// <returns>The short scale string.</returns>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optional) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion.
/// </param>
public static string ParseInt(int value, in int precision = 3, in int startShortScale = 1000000, in bool useSymbol = false)
{
string strVal = "";
string symbol = "";
ParseIntInternal(value, ref strVal, ref symbol, precision, startShortScale, useSymbol);
return strVal + " " + symbol;
}
/// <summary>
/// Parses an int value into short scale notation, splitting the data into the separate parts.
/// </summary>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static ShortScaleData ParseIntSplit(int value, in int precision = 3, in int startShortScale = 1000000, in bool useSymbol = false)
{
ShortScaleData data = new ShortScaleData();
ParseIntInternal(value, ref data.value, ref data.symbol, precision, startShortScale, useSymbol);
return data;
}
/// <summary>
/// Parses an int value into short scale notation, splitting the data into the separate parts.
/// </summary>
/// <param name="value">The input value that will be parsed.</param>
/// <param name="data">A referencce to an existing data to reuse the same object.</param>
/// <param name="precision">(Optional) The decimal precision that should be represented (subject to data type round off).
/// Default value is 3.</param>
/// <param name="startShortScale">(Optinal) Set the value to begin parsing to short scale. Default value is 1 million.</param>
/// <param name="useSymbol">
/// (Optional) use the single symbol list for more shortened notation. currently supports only to Decillion. Default value is false.
/// </param>
public static void ParseIntSplit(int value, ref ShortScaleData data, in int precision = 3, in int startShortScale = 1000000, in bool useSymbol = false)
{
ParseIntInternal(value, ref data.value, ref data.symbol, precision, startShortScale, useSymbol);
}
private static void ParseIntInternal(int value, ref string strVal, ref string symbol, in int precision = 3, in int startShortScale = 1000000, in bool useSymbol = false)
{
int index = -1;
int isNegative = 1;
string addPrecision = new string ('#', precision);
if (value < 0)
{
isNegative = -1;
value *= isNegative;
}
else if (!(value > 0))
{
strVal = "0";
symbol = "";
return;
}
if (value < 1000 || value < startShortScale)
{
strVal = (value * isNegative).ToString ("#,#");
symbol = "";
return;
}
int maxIndex = useSymbol
? ShortScaleSymbolReference.Length - 1
: ShortScaleReference.Length - 1;
while (value >= 1000 && index < maxIndex)
{
value = (int)((float)value * 0.001);
index++;
}
strVal = (value * isNegative).ToString ("#,#." + addPrecision);
symbol = useSymbol
? ShortScaleSymbolReference[index]
: ShortScaleReference[index];
}
}
/// <summary>
/// Allows for the value to be separated from the symbol; both can be styled separately.
/// </summary>
public struct ShortScaleData
{
///<summary>
/// The leading value of the short scale i.e., "123.45" from "123.45 million"
///</summary>
public string value;
///<summary>
/// The trailing symbol of the short scale i.e., "million" from "123.45 million"
///</summary>
public string symbol;
}
} |