--- a/UnitConverter.st Mon Jun 22 16:16:27 2015 +0200
+++ b/UnitConverter.st Mon Jun 22 16:22:35 2015 +0200
@@ -823,7 +823,8 @@
^ self
unitStringFor:nBytes
- scale:1024
+ scale:1024
+ rounded:false
unitStrings:#('' ' KiB' ' MiB' ' GiB' ' TiB' ' PiB' ' EiB' ' ZiB' ' YiB')
"/ the one below was written to save divisions.
@@ -984,6 +985,7 @@
^ self
unitStringFor:nBytes
scale:1000
+ rounded:false
unitStrings:#('' ' kB' ' MB' ' GB' ' TB' ' PB' ' EB' ' ZB' ' YB')
"/ |unitString n|
@@ -1126,6 +1128,7 @@
^ self
unitStringFor:hertz
scale:1000
+ rounded:false
unitStrings:#(' Hz' ' Khz' ' Mhz' ' Ghz' ' Thz' ' Phz' ' Ehz')
"/ |unitString n|
@@ -1191,9 +1194,10 @@
"Modified: 31.5.1996 / 18:08:20 / cg"
!
-unitStringFor:nBytes scale:k unitStrings:unitStrings
+unitStringFor:nBytes scale:k rounded:rounded unitStrings:unitStrings
"common helper for SI (1000-based) and binary (1024-based) human readable string generators.
- Notice that both forms are often encountered and useful."
+ Notice that both forms are often encountered and useful.
+ If rounded is true, round to nearest integer"
|n kN idx|
@@ -1208,12 +1212,21 @@
idx := idx + 1.
kN := kN * k.
].
+ rounded ifTrue:[
+ ^ ((nBytes / kN) rounded) printString , (unitStrings at:idx)
+ ].
^ ((nBytes / kN) asFixedPoint:1) printString , (unitStrings at:idx)
"
self fileSizeStringFor:nil
self fileSizeStringFor:10
self fileSizeStringFor:100
+ self fileSizeStringFor:900
+ self fileSizeStringFor:1100
+ self fileSizeStringFor:1400
+ self fileSizeStringFor:1499
+ self fileSizeStringFor:1500
+
self fileSizeStringFor:1000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' )
self fileSizeStringFor:8192 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' )
self fileSizeStringFor:10000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' )