This question already has answers here:
Math divison in Swift
(4 answers)
Closed 2 years ago.
I am facing a problem where I am not getting values of decimal places here is the code that I used to run in Swift Playground
print(100/1000)
print(Float(100/1000))
Expected Output :
0.10
0.10
Actual Output:
0
0.0
Because of this:
let variable = 100/1000
print(type(of: variable))
// prints Int
Just do the following:
print(100.0 / 1000.0)
print(Float(100) / Float(1000))
So in order to get a floating point result you need to divide floating point numbers instead of two integers
Related
This question already has an answer here:
why abs() function in dart return negative number when not wrapped in parenthesis?
(1 answer)
Closed 12 months ago.
I have an extension like;
extension x on num{}
and this extension contains below function;
double get wP {
assert(this >= 0.0, "value.wP: value can't be lower than 0.0!");
assert(this <= 1.0, "value.wP: value can't be bigger than 1.0!");
return (this.abs() * SizeService.instance.width).abs();}
SizeService.instance.width is a integer and it is = 50.
So, why -1.0.wp returning -50 ?
and I wan't to block all negative variable like;
-0.0 too but if I write assert like
assert(!this.isNegative, "Error bla bla");
it is not catching the negative value :(
so my question is here;
how can I block all negative and nan variables or if I can't do it, how can I convert all negative variables to positive ones ?
-0.0 is too.
because this.abs() is not working :/
thank u very much for any helpful answer!
-1.0.wP is in fact the same as -(1.0.wP)
Instead, try with (-1.0).wP
This question already has answers here:
Math divison in Swift
(4 answers)
Closed 3 years ago.
I am trying to figure out the percentages like so:
let percentages:CGFloat = CGFloat((result.count / 100) * 100)
But this always returns 0. What am I doing wrong?
result.count is 2.
The problem is that you're probably performing an integer division, so you need to convert that count to something else first:
let percentages = CGFloat(result.count) / 100 * 100
Notice, however that you're dividing and multiplying by the same value (100). You might need to tweak that too to achieve the desired result.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have func in my app which trims numbers, e.x. 9.81->9.8. But after updating swift behavior has changed.
let myNum = 9.7
print(round(myNum / 0.1) * 0.1)
Swift 4.1.2 output - 9.7
Swift 4.2 output - 9.700000000000001
Please advise how to resolve this issue. May it is swift's bug?
If you need the correct value itself, and not just a String representation, you can try this.
let value = 9.71
let roundedValue = round(value * 10) / 10
print(roundedValue)
It prints 9.7 on Swift 4.2, so I guess that the rounded value is correct and you can use it for additional computations.
This seems to be what you want:
let trimmedString = String(format: "%#.1f", round(myNum / 0.1) * 0.1)
This question already has answers here:
Convert Decimal to Binary Vector
(4 answers)
Closed 7 years ago.
If I use a decimal to binary command in Matlab how can I get the result in four digits? e.g. 7 =0111 or 2=0010.
Thanks in advance
Othman
Use dec2bin:
str = dec2bin(7) // 0111
str = dec2bin(7,8) // 00000111, pad the result to 8 bits
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!