Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculations for displaying percentage of budget remaining

Tags:

swift

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:

enter image description here

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.

like image 311
user3746428 Avatar asked Jan 09 '23 16:01

user3746428


1 Answers

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.

like image 72
Steve Rosenberg Avatar answered Jun 03 '23 10:06

Steve Rosenberg