Numbers and formatting

Andreas Raab andreas.raab at gmx.de
Tue Jun 21 17:30:10 UTC 2005


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
> 
> 
> 
> 




More information about the Squeak-dev mailing list