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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With