[Swift-user] round floating point numbers

Yadu Nand yadudoc1729 at gmail.com
Thu Dec 4 14:02:27 CST 2014


This is not a rounding solution,but it should give the same results you are
looking for :

foreach i in [1.0:9.0:1.0]{
    float foo = i/10;
    tracef("%f\n", foo);
}

Let me know if this doesn't work for you.

-Yadu


On Mon, Dec 1, 2014 at 3:15 PM, Ketan Maheshwari <ketan at mcs.anl.gov> wrote:

> Hi,
>
> Is it possible to round floating point numbers to just 1 or 2 digits of
> precision as opposed to the arbitrary precision as I am getting by default,
> eg:
>
> foreach i in [0.0:0.9:0.1]{
>   trace(i);
> }
>
> is giving:
>
> wiftScript trace: 0.0
> SwiftScript trace: 0.8999999999999999
> SwiftScript trace: 0.6
> SwiftScript trace: 0.7999999999999999
> SwiftScript trace: 0.4
> SwiftScript trace: 0.30000000000000004
> SwiftScript trace: 0.2
> SwiftScript trace: 0.7
> SwiftScript trace: 0.1
> SwiftScript trace: 0.5
>
> I need to round off values like 0.8999999 to 0.8 and so on.
>
> Thanks,
> Ketan
>
> _______________________________________________
> Swift-user mailing list
> Swift-user at ci.uchicago.edu
> https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-user
>



-- 
Yadu Nand B
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-user/attachments/20141204/0a74bd5c/attachment.html>


More information about the Swift-user mailing list