-1

I try on Xcode - Playground.

This is my code. Beginner.

======== import UIKit

var num1 : Double = 0.055  // Stock Price
var num2 : Double = 18     // Lots
var num3 : Double = 1000   // Share Per Lots

var sum1 : Double = num1 * num2 * num3    // Gross Share Price

var sum5 : Double = sum1 * (0.03/100) // Clearing Charges  // Answer Playground Return is " 0.297 "

My Questions is the "sum5" I want answer round up and display " 0.30 "

It is possible in swift code ?

Thanks.

2 Answers2

1

You can get it this way:

var roundOfSum5 : Double = Double(round(100 * sum5)/100)  //0.3
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0

Regarding to that answer

If you need to round to a specific place, then you multply by pow(10.0, numberOfPlaces), round, and then divide by pow(10, numberOfPlaces). In your case the number of places is 2.0:

let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
let rounded = round(sum5 * multiplier) / multiplier
print(rounded) // 0.3

If you have a number like sum5 = 0.3465 and you want to round to the third place after the decimal you can use 3.0 for numberOfPlaces and get as result 0.347

Community
  • 1
  • 1
ronatory
  • 7,156
  • 4
  • 29
  • 49