Hello,
I writing a little personal app which has parsed a directory tree and gathered the sizes of all the files (mp3s). I want to present it to myself in a nice format.
I don't want to see that my mp3 size is 61,599,788 bytes, but rather 58.7MB. I didn't find a method of conversion for such. I may have missed such.
So the only way I've really found to get the 58.7mb is kind of ugly. Below is my code.
------------------------------- niceByteSizesFromBytes: numberBytes
| mb |
mb := (numberBytes asFloat)/(1024*1024). mb := (((mb asScaledDecimal: 2) asString) allButLast: 2),'mb'.
^ mb -------------------------------
It works, it gives me what I want.
But is there a better way. I've scoured the Integer, Float protocols for appropriate methods. But alas I didn't see what I wanted. I am not a math or cs guy so I could have easily missed something. I don't know what floor and ceiling... are so... :)
Any words of wisdom greatly appreciated.
Jimmie
Try using #truncateTo: or #roundTo: instead of munging around with scaled decimals, e.g.,:
^((byteSize asFloat / (1024*1024)) truncateTo: 0.1) asString, 'mb'
Cheers, - Andreas
Jimmie Houchin wrote:
Hello,
I writing a little personal app which has parsed a directory tree and gathered the sizes of all the files (mp3s). I want to present it to myself in a nice format.
I don't want to see that my mp3 size is 61,599,788 bytes, but rather 58.7MB. I didn't find a method of conversion for such. I may have missed such.
So the only way I've really found to get the 58.7mb is kind of ugly. Below is my code.
niceByteSizesFromBytes: numberBytes
| mb |
mb := (numberBytes asFloat)/(1024*1024). mb := (((mb asScaledDecimal: 2) asString) allButLast: 2),'mb'.
^ mb
It works, it gives me what I want.
But is there a better way. I've scoured the Integer, Float protocols for appropriate methods. But alas I didn't see what I wanted. I am not a math or cs guy so I could have easily missed something. I don't know what floor and ceiling... are so... :)
Any words of wisdom greatly appreciated.
Jimmie
Hello Andreas,
Thanks for the help.
I tried using the truncateTo before but didn't get the results I was looking for.
looongNumber truncateTo: 1 looongNumber truncateTo: 2 looongNumber truncateTo: 3
All give strange results when you don't know what your doing. :)
You let me know I could use a decimal number 0.1. But I wanted two digits after decimal.
looongNumber truncateTo: 0.2 looongNumber truncateTo: 0.3
Also give strange results when you don't know what your doing. :)
But alas I stumbled upon looongNumber truncateTo: 0.01 Yeah! It works. So played some more and learned.
Then I searched the senders, found 5 that actually used it as such. And a bunch that I have no clue what kind of results they expect.
Thanks Andreas for sharing your wisdom and putting me on the trail toward enlightenment. It looks like a looong one. :)
Jimmie
Andreas Raab wrote:
Try using #truncateTo: or #roundTo: instead of munging around with scaled decimals, e.g.,:
^((byteSize asFloat / (1024*1024)) truncateTo: 0.1) asString, 'mb'
Cheers,
- Andreas
Jimmie Houchin wrote:
[snip original message]
Am 21.06.2005 um 21:02 schrieb Jimmie Houchin:
But alas I stumbled upon looongNumber truncateTo: 0.01 Yeah! It works. So played some more and learned.
Then I searched the senders, found 5 that actually used it as such. And a bunch that I have no clue what kind of results they expect.
I guess #roundTo: is used much more often in this context.
- Bert -
squeak-dev@lists.squeakfoundation.org