[Glass] 10.12345 round: 2 => 10.12 ?

Martin McClure martin.mcclure at gemtalksystems.com
Fri Nov 15 08:44:57 PST 2013


On 11/15/2013 06:20 AM, Mariano Martinez Peck wrote:
> Hi guys... mmmm In Pharo we have the message #round:. Here it is :
>
> Float >> round: numberOfWishedDecimal
>          "only leave a fixed amount of decimal"
>          "10.12345 round: 2 => 10.12"
>          | v |
>          v := 10 raisedTo: numberOfWishedDecimal.
>          ^ ((self * v) rounded / v) asFloat
>
>
> I see in GemStone Float has #roundTo: but doesn't seem to answer the
> same...
>

 From memory, #roundTo: is defined by ANSI to round to the nearest 
multiple of the argument -- so nnn roundTo: 2 would round to the nearest 
even number.

To round with two decimal places, you would use roundTo: 1/100.

Once again, that's from memory. Might be off a bit.

Regards,

-Martin



More information about the Glass mailing list