This question already has answers here:
Concatenate number with string in Swift
(7 answers)
Closed 3 years ago.
Hi i was trying to create a function that printed out the statement f(variable)degrees Fahrenheit = c(variable) degrees Celsius in swift and I am having issues printing it mainly cause its a variable with numbers and im trying to put it into a string I tried some different types of decleratino but it hasnt worked. and sorry for repetitive questions
var f = 68.0;
var c = (f - 32.0) * 5.0 / 9.0;
var conversion = "f degrees Fahrenheit = c degrees Celsius"
print(conversion)
You need to use String interpolation, otherwise it's just text.
Replace c with \(c) to actually get the value of variable c value.
Also f with \(f) for the same reason.
var conversion = "\(f) degrees Fahrenheit = \(c) degrees Celsius"
Related
This question already has answers here:
Is the Swift divide "/" operator not working or have I missed something?
(3 answers)
Division not working properly in Swift
(3 answers)
Closed 1 year ago.
I have created a for loop in which I calculate a few values.
for i in 1...100{
let xValue = i/100
print(xValue) // returns 0 every time except when i == 100
}
This is a recreation of a part of that for loop. Why is it that I do not get the right value for 'xValue'?
For info I have also tried the following:
let xValue: Float = Float(i/100)
And that doesn't work either, despite me being very specific. I must have forgotten something basic about these arithmetic
operators in swift.
When you divide an Int by an Int, the result will be rounded down. Use a floating point type, like Double or Float for more precision.
for i in 1...100 {
let xValue = Float(i)/100
print(xValue)
}
To address your attempted solution - when you do:
let xValue: Float = Float(i/100)
The Int result is first computed in i/100 (and rounded down to 0) then you are casting to a Float.
Therefore, we cast i to a Float before the division so the result is computed as a Float.
Since i and 100 are both integer values, / will do integer division and the result will be truncated to 0.
Even when you do let xValue: Float = Float(i/100), the result of division inside the parentheses is already truncated to 0 before the value can be converted to a Float.
Convert i to a floating-point value before dividing to prevent the result from being truncated.
for i in 1...100{
let xValue = Float(i)/100
print(xValue)
}
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.
This question already has answers here:
How to get Exponent of Scientific Notation in Matlab
(2 answers)
Closed 7 years ago.
testVector =
1.0e+10 *
3.5688 3.1110 5.2349
Is it possible to take out the exponent (not sure what it's called) of a vector and store it as a variable? E.g. in this case the variable would have value 1.0e+10
You can find the value of the exponent using log10:
testVector = [3.5688e+10 3.1110e+10 5.2349e+10];
lowExp = min(floor(log10(testVector)));
eVal = 10^lowExp;
Result:
eVal = 1.0000e+10
Then you'll need to divide your original vector by eVal:
newTestV = testVector/eVal
newTestV =
3.5688 3.1110 5.2349
This question already has answers here:
How to generate a random number in Swift?
(26 answers)
Closed 7 years ago.
How do I generate a random number in Swift language that can be used in math, such as addition. Basically I just want to be able to generate a random number and then add it to a count that I have.
For example, how would I generate a random number that I can add 1 to?
Use arc4random_uniform() function because it generates a uniform distribution.
The following line generates a number from 0-9.
var x = Int(arc4random_uniform(10)) // Cast it to Int because function returns UInt32
println(x)
var sum = 10 + x
println(sum)
Try to use this one & the arc4random function will generate value between 1-9 & it returns UInt32 type value so modify it what you wanna.
var count : UInt32 = 10
var value : UInt32 = arc4random()%10
count += value
print(count)
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!