Convert a CGPointo to Int in swift [duplicate] - swift

This question already has answers here:
Convert Float to Int in Swift
(15 answers)
Closed 7 years ago.
I'm building a class to handle with a basic graphic. I'm a newby and I'm on a self learning project to build small apps in Swift for IOS devices, since I found the language very easy to learn.
I've set in my class of type UIView two constants of type CGPoint:
let heigh = bounds.size.heigh
let width = bounds.size.width
These 2 variables are CGFloat, but I need them to be Int.
Appreciate help to convert them to these type value.

You should be able to convert the float value to an Integer like this:
let height = Int(bounds.size.height)

Related

How to stop rounding in NSDecimalNumber? [duplicate]

This question already has answers here:
In Swift 3, how to calculate the factorial when the result becomes too high?
(2 answers)
BigInteger equivalent in Swift?
(6 answers)
Closed 3 years ago.
I am solving a question from HackerRank which asks to print the value of extra-long factorials that can't be stored even in a 64-bit long variable.
I am using NSDecimalNumber to store the value. However, even in this case, the final result is rounded off.
func extraLongFactorials(n: Int) -> Void
{
var factorial: NSDecimalNumber = 1
for index in 1...n
{
let indexInNSDecimal = NSDecimalNumber(value: index)
factorial = factorial.multiplying(by: indexInNSDecimal)
}
let factorialWithoutRounding = factorial.description(withLocale: nil)
print(factorialWithoutRounding)
}
print(extraLongFactorials(n: 45)) // 119622220865480194561963161495657715064000000000000000000
However, the result should be 119622220865480194561963161495657715064383733760000000000.
This link talks about using description(withLocale:).
NSDecimalNumber round long numbers
However, it does not clearly explain how to use the description(withLocale:) method.
I also went through the apple doc https://developer.apple.com/documentation/foundation/nsdecimalnumber/1412789-description. But it also does not explain clearly how to use it.
Can someone please discuss this method in detail.

Issue in converting string to double [duplicate]

This question already has answers here:
swift: issue in converting string to double
(2 answers)
Closed 3 years ago.
I have mobile bank app.
When user type amount, then convert string to double i have problem
user typed amount example "8.7" is 8.699999999999999 and when i send request it sending 8.699999999999999
what can i do, to fix it?
I have tried this post:
swift: issue in converting string to double
var amount = "8.7"
var amountDouble = Double(amount)!
var amount = "8.7" . //"8.7"
var amountDouble = Double(amount)! //8.699999999999999
This imprecision is exactly why Double isn't an appropriate datatype for financial domains. Use Decimal instead, which is have perfect precision within its legal range.

Round off a Double to Two Decimal Points [duplicate]

This question already has answers here:
Swift double to string
(16 answers)
Closed 5 years ago.
I've written a simple swift program to show how much it costs to run electrical devices. The program works fine (all be it a little clunky - I'm new to swift!) but the result shows several figures after the decimal point so I've attempted to round it off to two decimal places. I'm not having much luck! My code is:
var pricePerkWh: Double = 13.426
var watts: Double = 5.0
var hours: Double = 730.0
var KW: Double = watts/1000
var kWh: Double = KW*hours
var penceMonth: Double = kWh*pricePerkWh
var poundMonth:Double = penceMonth/100
var cost = poundMonth.roundTo(places: 2)
print ("It will cost £\(cost) per month")
From what I've read here, roundTo(places: 2) is used but this resulted in the error
error: Power Costs.playground:6:12: error: value of type 'Double' has no member 'roundTo'
var cost = poundMonth.roundTo(places: 2)
Any pointers would be greatly appreciated!
Thanks
Double indeed has no method roundTo(places:). That‘s a method you would first have to implement yourself.
To do that, see this answer, for example.
Or, if you don’t want to create a separate method, you could directly do this (inspired by the aforementioned answer):
let cost = (poundMonth * 100).rounded() / 100
BUT:
If you don‘t need the rounded value for any further calculations, but want to display it to the user, NumberFormatter is the way to go. For example, it also offers localization. See this answer.

binary operator / cannot be applied to operands of type Int and Double [duplicate]

This question already has answers here:
Multiplying variables and doubles in swift
(2 answers)
So if string is not NilLiteralConvertible... what do some string functions return?
(1 answer)
Closed 7 years ago.
Hello brand new to Swift, and programming in general. Going through an exercise the code given is exactly:
//: Playground - noun: a place where people can play
import UIKit
let height = 12
let width = 10
let area = height * width
let areaInMeters = area / 10.762
But I get the error, "binary operator / cannot be applied to operands of type Int and Double".
After some digging around I found you can't operate on both an Integer and a Double. So I changed the last line to:
let areaInMeters = (Double)area / 10.762
Then I get the error, "Consecutive statements on a line must be separated by a ;" and it wants me to put the ; after area. None of this is making any sense to me.
Using El Capitan beta and Xcode 7 beta.
height and width will both be inferred as of type Int. Therefore area is also of type Int whilst 10.762 is a Double.
And in Swift safety is paramount so you'll need to have both operands of same type.
Solution is (as Eric D. suggested) is to convert area to a Double:
let areaInMeters = Double(area) / 10.762
Try instead adding a decimal point and a zero to the end of your height and width.
Like so:
let height = 12.0
let width = 10.0
And you won't have to worry about having to deal with an Integer.
Hope this helps. Happy Coding!

Swift: Trunc a floating number to show it in a label [duplicate]

This question already has answers here:
String formatting of a Double [duplicate]
(2 answers)
Closed 8 years ago.
When I wanted to trunc a floating number for showing in a label in Objective-C I could just use LABEL.text = [NSString stringWithFormat:#"%.3f", FLOAT];
Though I can't find how to do so in Swift.
The old NSString format is still available. You can use that.
let myfloat : Float = 1.23
LABEL.text = NSString(format: "%.3f", myfloat)