Calculations to display the percentage of remaining budget

I have just added a circular progress bar to my application that I intend to use to display the percentage of users' budget that they have spent.

I was struggling to figure out how I would convert the current budget calculation to percentages to use to customize the progress circle. Basically, I have a total expense (expense) counter and a total credit (receipts) score and I need to turn that into a number like 0.134 for the progress circle, and I also need to figure out what percentage was spent.

Here is a picture of my circular motion:

enter image description here

This is my code for calculating the current budget:

currentBudgetCalculation = currencyDouble + totalCreditCounter - totalSpendingsCounter

      

currencyDouble is the starting budget, totalCreditCounter is the total, and totalSpendingsCounter is the total costs.

What calculation do I need to do to get this to work? The variable for entering the run circle value is CGFloat.

+3


source to share


1 answer


Depending on what you want to show, you can use any formula below.

1) this is for calculating expenses as a percentage of the starting budget plus loans

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 expenses as a percentage of the 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)

      

Variables could also be Doubles.

+6


source







All Articles