Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ByteFormat.c
Go to the documentation of this file.
1/*
2Useful functions to handle byte size formatting
3*/
4
6{
7 const static float BYTE_UNIT_SIZE = 1024.0;
8
9 const static string FORMAT_B ="#AR-DataSize_B";
10 const static string FORMAT_KB ="#AR-DataSize_kB";
11 const static string FORMAT_MB ="#AR-DataSize_MB";
12 const static string FORMAT_GB = "#AR-DataSize_GB";
13
14 //------------------------------------------------------------------------------------------------
16 static string ReadableSizeFromBytes(out float value, int decimals = 1)
17 {
18 const int safeCount = 4;
19 int count = 0;
20
21 float valCheck = value;
22
23 while (value > BYTE_UNIT_SIZE && count < safeCount)
24 {
25 value /= BYTE_UNIT_SIZE;
26 count++;
27 }
28
29 // Cut last 1000
30 if (value * 0.001 > 1)
31 {
32 value *= 0.001;
33 count++;
34 }
35
36 valCheck = value;
37
38 // First decimal place
39 if (decimals > 0)
40 {
41 float dec = Math.Pow(10, decimals);
42 float valueMult10 = Math.Ceil(value * dec);
43
44 if (valueMult10 <= dec)
45 value = valueMult10 / dec;
46 else
47 value = valueMult10 / dec;
48 }
49
50 // Add proper metrics
51 switch (count)
52 {
53 // byte
54 case 0: return FORMAT_B; break;
55
56 // kilo
57 case 1: return FORMAT_KB; break;
58
59 // mega
60 case 2: return FORMAT_MB; break;
61
62 // giga
63 case 3: return FORMAT_GB; break;
64 }
65
66 return "";
67 }
68
69
70 //------------------------------------------------------------------------------------------------
73 static string GetReadableSize(float value, int decimals = 1)
74 {
75 string unit = SCR_ByteFormat.ReadableSizeFromBytes(value, decimals); // format %1 unit
76 return WidgetManager.Translate(unit, value);
77 }
78
79 //------------------------------------------------------------------------------------------------
82 static string GetReadableSizeMb(float value)
83 {
84 float mb = 1024.0*1024.0;
85 float sizeMb = value / mb;
86
87 if (sizeMb < 1.0)
88 {
89 int sizeMbMult10 = Math.Ceil(sizeMb * 10.0);
90
91 if (sizeMbMult10 <= 10)
92 sizeMb = sizeMbMult10 / 10;
93
94 return WidgetManager.Translate(FORMAT_MB, sizeMb);
95 }
96 else
97 return WidgetManager.Translate(FORMAT_MB, Math.Round(sizeMb));
98 }
99
100 //------------------------------------------------------------------------------------------------
102 static string ContentDownloadFormat(float value)
103 {
104 string unit = SCR_ByteFormat.ReadableSizeFromBytes(value, 3);
105 string size = value.ToString();
106
107 int dec = 3;
108
109 if (unit != SCR_ByteFormat.FORMAT_GB)
110 dec = 1;
111
112 size = value.ToString(-1, dec);
113 return WidgetManager.Translate(unit, size);
114 }
115};
int size
Definition Math.c:13