[Swift-user] round floating point numbers

Ketan Maheshwari ketan at mcs.anl.gov
Mon Dec 1 15:15:51 CST 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-user/attachments/20141201/27f4a653/attachment.html>


More information about the Swift-user mailing list