Round Double value in swift with addition before decimal [closed] - swift

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i have double value 70514.94971385633, now I want it to round the value and make it 70515.00. I have tried command rounded(), but it only rounds value after the decimal. How I can round value after the decimal and add the nearest value to a number before decimal? this is my code for rounding the value,
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded()
but it shows result 70514.95, i want it to add it before decimal value like 70515.00

Just small change in your code
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.round()
or
let roundedValue = totalBidValue.rounded(.up)

Use ceil(_:) to get that working,
let value = 70514.94971385633
let result = ceil(value)
print(result) //70515.0

Use round()
let myDoubleValue = 70514.94971385633
let roundedOffValue = round(myDoubleValue)
print(roundedOffValue) // 70515.0

Your code is almost perfect...
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded(.toNearestOrAwayFromZero)
just add .toNearestOrAwayFromZero in your code

Related

Dynamic trim double [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What I want.
A: $1.10 = $1.1
B: $0.10101010101 = $0.101010
What I've tried
Solution one -
String(format: "%.6f", price)
This will do the following:
A: $1.100000
B: $0.1010101
So B is gets the correct outcome but A gets more decimals.
Solution two -
numberFormatter.numberStyle = .decimal
This gives the following outcome
A: $1.1
B: $0.1
So here A is correct but B gets rounded up.
This code will work for removing trailing zeros
let distanceFloat1: Float = 1.10
let distanceFloat2: Float = 0.10101010101
print("Value \(distanceFloat1.clean)")
print("Value \(distanceFloat2.clean)")
extension Float {
var clean: String {
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
}
}
Output
Value 1.1
Value 1.101010
if you want to remove trailing zeros, also want to remove decimals after x place use
Swift - Remove Trailing Zeros From Double
I removed the trailing zeros using Ben's answer.
var stringWithoutZeroFraction: String {
return truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
}

how do i approach "h" parameter to zero in derivative? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a few problem with my derivative because it shows me an error when I approach Denominator to zero .
func derivativeOf(fn: (Double) -> Double, atX x: Double) -> Double {
let h -> 0
return (fn(x + h) - fn(x))/h
}
i know my syntax sucks but currently it is common in calculus and mathematical Differential.
You can't express "number approaching zero" as let h -> 0 - this is just an invalid syntax in Swift.
There's also no specific operator for "number approaching zero". But depending on what you need, you could for example express "smallest possible positive number", using Double.leastNonzeroMagnitude:
let h = Double.leastNonzeroMagnitude

Display only some digits of an Int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Let's say you have an Int.
let int = 12345
I want it to only display some of the digits.
For example: print(firstTwoDigits) --> 12
How do I do this and thank you in advance.
It depends on your specific requirements.
This prints the first two digits of an integer number
let intVal = 12345
print(String(intVal).prefix(2))
Output: 12
Another way which only prints certain ones in the number:
let intVal = 12345
let acceptableValues = ["1", "2"]
let result = String(intVal).filter {
acceptableValues.contains(String($0))
}
print(result)
Output: 12

Swift 3 - matching user input with dictionary values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
The user of my app will be tested on their english. They will select different words from a list by placing a check mark next to the word. They will want to ONLY select the words that are nouns. If, for example, they choose 5 out of 10 correctly, how do I show them a score of 50%. I think that I need to filter dictionary values based on the user's input. The user's input being an array. What is the best way to code this?
Try the following code:
let myDictionary : [String : Any] =
[ "Nouns":["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"],
"Verbs":["Eat","play","dance","walk","run","sing","read","write","go","come"]]
var myUserSelectedArray:[String] = ["One","come","Three","go","Five","x","Six","x","x","Ten"];
let myNounArray = myDictionary["Nouns"] as? [String];
let myVerbArray = myDictionary["Verbs"] as? [String];
let set1:Set<String> = Set(myUserSelectedArray);
let set2:Set<String> = Set(myNounArray!);
let set3:Set<String> = Set(myVerbArray!);
let scroeInNoun = set1.intersection(set2).count;
let scroeInVerb = set1.intersection(set3).count;
print ("score in Verb \((scroeInVerb * 100)/set2.count) %")
print ("score in noun \((scroeInNoun * 10)/set3.count ) %")
let finalScore = (scroeInNoun + scroeInVerb) * 100 / ((myNounArray?.count)! + (myVerbArray?.count)!)
print ("final score in noun \(finalScore) %")

Why dosn't this objective-c if statement work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I's sorry but I'm new to Xcode and coding.
Im trying to create an If statement so that if label 3 = or greater then 20 label 4 = 0, else if lower then 20 it = 1 and if lower then 18.5 its = 2.
this is my code for that:
if (_label3.text >= #"20") {_label4.text = #"0";}
else if (_label3.text < #"20") {_label4.text = #"1";}
else if (_label3.text <= #"18.5") {_label4.text = #"2";}
I'm not sure what is going wrong, but i am getting this error 'Direct comparison of String literal has undefined behavior' and Xcode wont let me build the app.
Thank for your Help
you are using arithmetic operations on strings. that for sure makes no sense
create a float from the textfield input
if ([_label3.text floatValue] >= 20.0) {_label4.text = #"0";}
aslo you have to change the 1st and 2nd else branch, a the last one will never be called, as if it is true, the fist one is also be true.
float value = [_label3.text floatValue];
if (value > 20.0) {_label4.text = #"0";}
else if (value <= 18.5) {_label4.text = #"2";}
else if (value < 20.0) {_label4.text = #"1";}
label.text is string value and you cant compare string to int. Change the string to int and then compare like this
if([_label3.text intValue] >= 20){
_label4.text = #"0";
}
Hope this helps.