So, I have 14 checkmarks in total and I want to press one and have a number calculate the overall completion for all the checkmarks(14 in total).
I researched through the web and I tried to solved it like this...First, I thought I needed to declare two variables; one for the counter and another for the total (14) , then I declare a constant (which would represent my total value * 100 , and I will finally do the percentage calculation.
My problem comes when I have to define what the overall percentage is calculated.
Code is here:
import UIKit
var checkmarksDouble: Float = 100.0 //this is my total on percentage
var totalCheckmarksCounted: Float = 14.0 //my total on checkmarks
var counter: Float = + 1 //Counter
let perCent = 100*counter/(checkmarksDouble + counter)
var perCentCGFloat = CGFloat(perCent)
Any suggestions?
if u want to calculate total percentage according to checkmark.
var checkmarksDouble: Float = 100.0 //this is my total on percentage
var totalCheckmarksCounted: Float = 14.0 //my total on checkmarks
var counter: Float = 0 //Counter
var perCentCGFloat:Float = 0
var percentageOfOneCheckmark:Float = 0
now calculate percentage for single checkmark
percentageOfOneCheckmark = checkmarksDouble / totalCheckmarksCounted
call this method when you click checkmark
func checkmarkClickForCalculaationPercentage() -> Void
{
counter += 1
perCentCGFloat = percentageOfOneCheckmark *counter
}
Related
This question already has answers here:
How to round a Double to the nearest Int in swift?
(9 answers)
Closed 4 years ago.
I try to use swift code to calculate 10 * 75% = 7.5
var b : Int = 10
var a : Int = 75
// result x is 0
var x = b * (a / 100)
However the result is zero. How to get 7.5 result without changing the type and value of a and b?
UPDATE:
I got it right by:
var x: Double = (Double(b) * (Double(a) / 100)) // x is 7.5
Now, how can I round it to 8 as a Int type?
You're using integer (i.e. whole number) arithmetic, when what you want is floating-point arithmetic. Change one of the types to Float, and Swift will figure out that x is also a Float.
var b : Int = 10
var a : Int = 75
var x = Float(b) * (Float(a) / 100.0) // now x is a Float (.75)
For doing arithmetic in Swift, both sides of the equation must be using the same type. Try using this code:
let b = 10.0
let a = 75.0
let x = b * (a / 100.0
print(x)
7.5
To make it round up, use the built in ceil function
print(ceil(x))
8.0
Ok then you just need to do:
var b : Int = 10
let c = (Double(b) * 0.75)
let restult = ceil(c)
I have following numbers in CGFloat
375.0
637.0
995.0
I need to get the first number in CGFloat data type. For example the result for #1 must be 3.0, for #2 must be 6.0 and #3 must be 9.0
I tried the following
let width:CGFloat = 375.0
// Convert Float To String
let widthInStringFormat = String(describing: width)
// Get First Character Of The String
let firstCharacter = widthInStringFormat.first
// Convert Character To String
let firstCharacterInStringFormat = String(describing: firstCharacter)
// Convert String To CGFloat
//let firstCharacterInFloat = (firstCharacter as NSString).floatValue
//let firstCharacterInFloat = CGFloat(firstCharacter)
//let firstCharacterInFloat = NumberFormatter().number(from: firstCharacter)
Nothing seems working here. Where am I going wrong?
Update
To answer #Martin R, find below my explanation
I am implementing a grid-view (like photos app) using UICollectionView. I want the cells to be resized based on screen size for iPhone/iPad, Portrait and Landscape. Basically I don't want fixed columns. I need more columns for larger screen sizes and lesser column for smaller screen sizes. I figured that perhaps I can decide based on screen width. For example if the screen width is 375.0 then display 3 columns, If somewhere around 600 then display 6 columns, if around 1000 then display 10 columns and so on with equal width. So what I came up with is a) decide columns based on first number of the screen size and then for width divide by actual screen width. For example for a screen width of 375.0 I will have a cell size of CGSize(width: screenWidth / totalColumn) and so on.
You said:
For example if the screen width is 375.0 then display 3 columns, If somewhere around 600 then display 6 columns, if around 1000 then display 10 columns and so on with equal width.
So what you really want is not the first digit of the width (which would
for example be 1 for width = 1024 instead of the desired 10)
but the width divided by 100 and rounded down to the next integral value:
let numColumns = (width / 100.0).rounded(.down)
Or, as an integer value:
let numColumns = Int(width / 100.0)
var floatNum:CGFloat = 764.34
var numberNeg = false
if floatNum < 0{
floatNum = -1.0 * floatNum
numberNeg = true
}
var intNum = Int(floatNum)
print(intNum) //764
while (intNum>9) {
intNum = Int(intNum/10)
}
floatNum = CGFloat(intNum)
if numberNeg {
floatNum = -1.0 * floatNum
}
print(intNum)//7
print(floatNum)//7.0
try this one ...I hope it'll work
I need to create a script that will calculate the overlap orbital of two 1s orbitals. The integral is given by
I tried calculating this using code but my answer is nowhere near the analytic result of S=(1+R+R^2/3)exp(-R). Could someone help me figure where I went wrong?
The code:
import Foundation
var sum: Double = 0.0 //The integral result
var step_size: Double = 0.0000025
var a: Double = 0.0
var R: Double = 5.0
var next_point: Double = 0.0
var midpoint: Double = 0.0
var height: Double = 0.0
var r_val: Double = 0.0
func psi_func(r_val: Double) -> Double {
return exp(-r_val)
}
//Integration
while next_point < R {
next_point = a + step_size
midpoint = a + step_size/2
height = psi_func(r_val: midpoint)
sum += psi_func(r_val: midpoint)*step_size
a = a + step_size
}
print("S = ", 2*3.14159*3.14159*sum) // This is a 3-D orbital, so I multiply by 2*pi*pi
For R = 5.0
My answer: 19.61
Analytic answer: 0.097
Two problems I can see:
I see a single wavefunction and not the product of two in your code
It is incorrect to just do a 1d integral and multiply by 2pi^2 in the end
Try doing a proper 3d integral with the correct integrand.
I have a just added a circular progress bar to my app that I intend to use to display the percentage of a users budget that they have spent.
I have been struggling to figure out how I would convert the current budget calculation into a percentage to use to configure the progress circle. Basically, I have a total expenditure counter (outgoings) and a total credit counter (incomings) and I need to turn this into a number such as 0.134 for the progress circle, and I also need to figure out what percentage has been spent.
Here is an image of my progress circle:
This is my code for calculating the current budget:
currentBudgetCalculation = currencyDouble + totalCreditCounter - totalSpendingsCounter
currencyDouble is the initial budget, totalCreditCounter is the the total incomings and totalSpendingsCounter is the total outgoings.
What calculation would I need to do to get this working? The variable for inputing the progress circle value is a CGFloat.
Depending on what you want to show you could use either formula below.
1) this is for calculating spending as a percent of initial budget plus credits
import UIKit
var currencyDouble: Float = 100.0
var totalCreditCounter: Float = 10.0
var totalSpending: Float = 30.0
let perCent = 100*totalSpending/(currencyDouble + totalCreditCounter)
var perCentCGFloat = CGFloat(perCent)
2) this is for calculating spending as a percent of initial
import UIKit
var currencyDouble: Float = 100.0
var totalCreditCounter: Float = 10.0
var totalSpending: Float = 30.0
let perCent = 100*totalSpending/currencyDouble
var perCentCGFloat = CGFloat(perCent)
The variables could have been Doubles also.
So here is my code:
int var1 = serverList[i].webUp;
int var2 = serverList[i].webUp + serverList[i].webDown;
double ratio = (double)var1 / var2;
ratio = ratio * 100.0;
string percentage = string.Format("Up time is {0:P2}%", ratio);
Every time I run my application I keep getting NaN instead of a percent. webUp and webDown are both int, and I'm type casting to double when I do the division. Any ideas? I'm not dividing by 0, all values are greater than 0 and never in the negative. I keep getting "Up time is NaN%"