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.
Related
This question already has answers here:
Concatenate Swift Array of Int to create a new Int
(5 answers)
Closed 2 years ago.
I have an [Int] array like so:
[1, 2, 3]
How can I apply a function on this so it returns:
123
?
let nums = [1, 2, 3]
let combined = nums.reduce(0) { ($0*10) + $1 }
print(combined)
Caveats
Make sure the Int won't overflow if the number gets too long (+263 on a 64-bit system).
You need to also be careful all numbers in the list aren't more than 9 (a single base-10 digit), or this arithmetic will fail. Use the String concatenation technique to ensure that all base-10 numbers are correctly handled. But, again, you need to be careful that the number won't overflow if you choose to convert it back to an Int.
We can do like below...
var finalStr = ""
[1,2,3].forEach {
finalStr.append(String($0))
}
if let number = Int(finalStr) {
print(number)
}
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:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 6 years ago.
Is there a shorter way to create an array of numbers spanning a range in swift?
Right now, I'm using this:
var arrayOfInts = [UInt32]()
for number in 1...100 {
arrayOfInts.append(number)
}
Is there a one-line way of doing it?
var arrayOfInts = Array(1...100)
Playground Output
Is this short enough?
let array = Array(1...100)
Try like this
var z = [Int](1...100)
print(z)
DEMO
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)
This question already has answers here:
Swift equivalent for MIN and MAX macros
(5 answers)
Closed 8 years ago.
I'd like to assign the lesser of two values to a variable. In Ruby I would do something like:
my_var = [value_one, value_two].min
In Swift, of course, I can do this:
var myVar = 0.0
if valueOne < valueTwo {
myVar = valueOne
} else {
myVar = valueTwo
}
But, I'm wondering if there is a cleaner, more succinct solution.
var myVar = min(valueOne, valueTwo)
min is a standard library function that takes the lesser of two (or least of several — it's variadic) Comparable values.