How to round a float?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to round a float?

Costas
Hi,

I was looking for a method to round a number.
Is this the best way to get a rounded number to 2 decimal places?

number := 12.345.
res := ReadWriteStream with: (String new: 0).
number printOn: res decimalPlaces: 2.
number := res contents asNumber.

Thanks

Costas


Reply | Threaded
Open this post in threaded view
|

Re: How to round a float?

Ian Bartholomew-17
Costas,

> I was looking for a method to round a number.

Try

123.456789 roundTo: 0.01 ====> 123.46

or

123.456789 truncateTo: 0.01 ====> 123.45

Regards
    Ian