|
I think this would generalize the solution
<br />
size_t formatter(double dNumber, TCHAR* pszBuffer, size_t nSize) {<br />
::ZeroMemory(pszBuffer, nSize);<br />
int iDepth = 0;<br />
<br />
double dResult = _formatter(dNumber, iDepth);<br />
_stprintf(pszBuffer, _T("%.02f"), dResult);<br />
switch (iDepth) {<br />
case 0: _tcscat(pszBuffer, _T(" B") );break;<br />
case 1: _tcscat(pszBuffer, _T(" KB") ); break;<br />
case 2: _tcscat(pszBuffer, _T(" MB") ); break;<br />
case 3: _tcscat(pszBuffer, _T(" GB") ); break;<br />
case 4: _tcscat(pszBuffer, _T(" TB") ); break;<br />
case 5: _tcscat(pszBuffer, _T(" PB") ); break;<br />
case 6: _tcscat(pszBuffer, _T(" EB") ); break;<br />
}<br />
<br />
return _tcslen(pszBuffer);<br />
}<br />
<br />
double _formatter(double dNumber, int& iDepth) {<br />
if (dNumber < 1024.0) {<br />
return dNumber;<br />
} else {<br />
return _formatter(dNumber / 1024.0, ++iDepth);<br />
}<br />
}<br />
---
Mina A. Victor
|
|
|
|
|
Hello,
i'm sorry, i'm not sending u about ur project, but i didn't find ur email to contact u. i'm interrested in embroidery, i'm implementing a project in embroiderey and i need ur help plz.
i want to implement code to read and write embroidery files. so would u plz send me any available embroidery files formats to implement them.
to contact me : mmhafez@hotmail.com
thx for ur cooperation
yours sincerely
Eng. Mohamed M. Hafez
|
|
|
|
|
This is a useful article! But you may want to consider using ::GetNumberFormat() .
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Nice article, congratulations!
There is another way to accomplish this if you are willing to link to shlwapi.lib (and therefore create a dependency on shlwapi.dll). It contains the function StrFormatByteSize(DWORD dw, LPSTR pszBuf, UINT cchBuf ); which I believe does at least very similar job.
The functions inside shlwapi.dll are often overlooked - there are some very handy functions in there!
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!
|
|
|
|
|
dabs wrote:
StrFormatByteSize(DWORD dw, LPSTR pszBuf, UINT cchBuf );
Thanks for the hint. I was sure there must be a standard way to do this . However, the API does not provide the possibility to control the format, which is possible through altering the above code. I am also planning to provide some customisation features in future versions
Some compatability information from MSDN (there is no mention of XP or Me):
shlwapi.dll version 4.71 or later
Windows 2000, Windows NT 4.0 with Internet Explorer 4.0, Windows 98, Windows 95 with Internet Explorer 4.0
Regards,
Victor
phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]
|
|
|
|
|
MSDN say:
Note 2: All systems with Internet Explorer 4.0 or 4.01 will have the associated version of Comctl32.dll and Shlwapi.dll (4.71 or 4.72, respectively). However, for systems prior to Windows 98, Internet Explorer 4.0 and 4.01 can be installed with or without the integrated Shell. If they are installed with the integrated Shell, the associated version of Shell32.dll will be installed. If they are installed without the integrated Shell, Shell32.dll is not updated. In other words, the presence of version 4.71 or 4.72 of Comctl32.dll or Shlwapi.dll on a system does not guarantee that Shell32.dll has the same version number. All Windows 98 systems have version 4.72 of Shell32.dll.
So, any system with at least IE4.0 will have that function available.
Victor Boctor wrote:
However, the API does not provide the possibility to control the format, which is possible through altering the above code.
For what? There is a standard way to do it. Not that I want to suppress the value of your code, but someone (MS) has done it already. And it seems to work pretty fine.
And it always produces the same result as seen in Windows Explorer.
There are, by the way, 3 functions to accomplish this: StrFormatByteSize(), StrFormatByteSize64() and StrFormatKBSize().
I don't think this is a serious possesion, and the evil most likely comes from your hand. Colin J Davies, The Lounge
|
|
|
|
|
Instead of just using "," as thousand separator, it would be great if you took it from windows' locale settings.
Where I live we use "." as thousand separator
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
Anders Molin wrote:
Instead of just using "," as thousand separator, it would be great if you took it from windows' locale settings.
This is one of the planned enhancements (see Future Improvements section). Hopefully will include it soon.
Regards,
Victor
phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]
|
|
|
|
|
Another item for the "to do" list is the support for 64-bit file sizes. A single DWORD may not be enough for some huge files (such as AVI captures). I think a ULARGE_INTEGER or ULONGLONG should be fine, but I don't know which is supposed to be the standard fo file sizes, especially on a 64-bit Windows platform.
Paolo
------
"airplane is cool, but space shuttle is even better" (J. Kaczorowski)
|
|
|
|
|
Sounds like a good idea. I will add this to the "To Do" list. Is there an advantage for ULARGE_INTEGER or ULONGLONG over __int64?
I will also add support for Terabyte.
Regards,
Victor
phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]
|
|
|
|
|
ULARGE_INTEGER is a struct that works on compilers that does not support the 64-bit integers internally, where ULONGLONG is just a (Windows) typedef.
|
|
|
|
|
|
Paolo Messina wrote:
Yeah, and don't forget the others. See: http://www.tuxedo.org/~esr/jargon/html/entry/quantifiers.html[^]
Thanks for the interesting link. I extracted the related part here:
kilo- 1000^1 1024^1 = 2^10 = 1,024
mega- 1000^2 1024^2 = 2^20 = 1,048,576
giga- 1000^3 1024^3 = 2^30 = 1,073,741,824
tera- 1000^4 1024^4 = 2^40 = 1,099,511,627,776
peta- 1000^5 1024^5 = 2^50 = 1,125,899,906,842,624
exa- 1000^6 1024^6 = 2^60 = 1,152,921,504,606,846,976
zetta- 1000^7 1024^7 = 2^70 = 1,180,591,620,717,411,303,424
yotta- 1000^8 1024^8 = 2^80 = 1,208,925,819,614,629,174,706,176
The article will be updated to support Terabyta (TB), Petabyte (PB), and Exabyte (EB).
Thanks,
Victor
phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]
|
|
|
|
|
I think there are too many numbers in some of the examples to make it easy to read. I would try to limit the precision to two value numbers.
IMHO if you have 1019-1075 bytes that should be presented as 1.0 kB, if you have 1009-1018 bytes that should be presented as 0.99 kB.
|
|
|
|
|
Dalle wrote:
IMHO if you have 1019-1075 bytes that should be presented as 1.0 kB, if you have 1009-1018 bytes that should be presented as 0.99 kB.
Can you provide the criteria/algorithm?
I had a look at the Windows Explorer to get some ideas. I found that it does not display any decimal points, which looks good. However, it might not be accurate enough for some applications. Hence, I decided to add to the "Future Improvements", the support for an accuracy parameter which specifies the required number of decimal places.
Regards,
Victor.
phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]
|
|
|
|
|
If done this via division with 102. It is the next iteration for 1024/10
Try this @ home. (B&B)
|
|
|
|
|
Victor Boctor wrote: I had a look at the Windows Explorer to get some ideas. I found that it does not display any decimal points, which looks good.
Only in the "Size" column. If you take a look in the status bar, it shows exactly 3 significant digits for files 1KB or larger:- 1.07 GB
- 460 MB
- 34.0 KB
- 1023 bytes
They're probably using some kind of black magic to do it, though.
EDIT: Actually, I found it sometimes uses just 2 sigfig for sizes slightly below 1.00 _B, e.g. 999 KB is displayed as such, but 1000 KB is displayed "0.97 MB."
A little learning is a dangerous thing;
Drink deep, or taste not, the Pierian Spring.
—Alexander Pope
modified on Tuesday, September 2, 2008 6:48 PM
|
|
|
|
|