11using System . Text ;
2- using UnityEngine ;
32
43namespace Zigurous . DataStructures
54{
@@ -8,48 +7,6 @@ namespace Zigurous.DataStructures
87 /// </summary>
98 public static class StringExtensions
109 {
11- /// <summary>
12- /// Represents how a number is abbreviated to a string.
13- /// </summary>
14- [ System . Serializable ]
15- public struct NumberAbbreviation
16- {
17- /// <summary>
18- /// The number factor after which the abbreviation is applied.
19- /// </summary>
20- [ Tooltip ( "The number factor after which the abbreviation is applied." ) ]
21- public float factor ;
22-
23- /// <summary>
24- /// The string format of the abbreviated number.
25- /// </summary>
26- [ Tooltip ( "The string format of the abbreviated number." ) ]
27- public string format ;
28-
29- /// <summary>
30- /// Creates a new number abbreviation with the given
31- /// <paramref name="factor"/> and <paramref name="format"/>.
32- /// </summary>
33- /// <param name="factor">The number factor after which the abbreviation is applied.</param>
34- /// <param name="format">The string format of the abbreviated number.</param>
35- public NumberAbbreviation ( float factor , string format )
36- {
37- this . factor = factor ;
38- this . format = format ;
39- }
40-
41- }
42-
43- /// <summary>
44- /// A pre-defined set of number abbreviations.
45- /// </summary>
46- private static readonly NumberAbbreviation [ ] abbreviations = new NumberAbbreviation [ 4 ] {
47- new NumberAbbreviation ( 1_000_000_000_000 , "0T" ) ,
48- new NumberAbbreviation ( 1_000_000_000 , "0B" ) ,
49- new NumberAbbreviation ( 1_000_000 , "0M" ) ,
50- new NumberAbbreviation ( 1_000 , "0K" ) ,
51- } ;
52-
5310 /// <summary>
5411 /// A reusable string builder.
5512 /// </summary>
@@ -92,39 +49,6 @@ public static string Repeat(this string str, int n)
9249 return stringBuilder . ToString ( ) ;
9350 }
9451
95- /// <summary>
96- /// Converts the number to an abbreviated string.
97- /// </summary>
98- /// <param name="number">The number to abbreviate.</param>
99- /// <returns>A new string of the abbreviated number.</returns>
100- public static string ToAbbreviatedString ( this float number )
101- {
102- return ToAbbreviatedString ( number , StringExtensions . abbreviations ) ;
103- }
104-
105- /// <summary>
106- /// Converts the number to an abbreviated string using the provided
107- /// <paramref name="abbreviations"/>.
108- /// </summary>
109- /// <param name="number">The number to abbreviate.</param>
110- /// <param name="abbreviations">The abbreviations that can be applied to the number.</param>
111- /// <returns>A new string of the abbreviated number.</returns>
112- public static string ToAbbreviatedString ( this float number , NumberAbbreviation [ ] abbreviations )
113- {
114- float abs = System . Math . Abs ( number ) ;
115-
116- for ( int i = 0 ; i < abbreviations . Length ; i ++ )
117- {
118- NumberAbbreviation abbreviation = abbreviations [ i ] ;
119-
120- if ( abs >= abbreviation . factor ) {
121- return ( number / abbreviation . factor ) . ToString ( abbreviation . format ) ;
122- }
123- }
124-
125- return number . ToString ( ) ;
126- }
127-
12852 }
12953
13054}
0 commit comments