Math Equation in Ticket App Not Formatting Correctly - swift

I’m having a difficult time formatting my math equation to calculate and format correctly. When I try to add the child ticket to the total price function, it formats the total price label very oddly. It formats the tickets totals separately instead of together. It has something to do with my function for calculating price.
My total price block of code is as follows:
totalPriceLabel.text = String(adultTicket.price * Double(adultTicketsSelected)) + String(childTicket.price * Double(childTicketsSelected))
Any help is appreciated.

to keep it simple while you get the hang of things, calculate the value first:
let price = adultTicket.price * Double(adultTicketsSelected) + childTicket.price * Double(childTicketsSelected)
You could use string interpolation to then convert this to your text:
totalPriceLabel.text = "\(price)"
but you'd be better using either a number formatter or String(format:)
totalPriceLabel.text = String(format: "%.2f", price)
I'll let you research the NumberFormatter yourself. :-)

Related

How to align numbers vertically? Swift, UIKit

I want to align numbers by digits in table rows. For example:
___123.4
-5 678.9
That is, so that tens are under tens, units under units, a fractional number under a fractional number.
To convert a Number to a String, I use the numberStringFormatter function.
func numberStringFormat(_ number: Double) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 1
numberFormatter.groupingSeparator = " "
let result = numberFormatter.string(from: NSNumber(value: number))
return result ?? ""
}
This function sets the fractional format, determines the number of decimal places, and groups the numbers before the decimal point by digits.
But if the number is an integer, without fractions, or a digit after a floating point after rounding it turns out to be 0, then the formatted string looks like this, for example, 123
And then these numbers in the rows of the table are shifted and it turns out like this:
----123
5 678.9
That is, the fractional number on the bottom row is under the integer number on the top row.
In my opinion, I can solve this task if I force the Number to always show 0 after converting to a String.
I tried googling but couldn't find an answer to this question.
Maybe someone has already encountered such situations and can suggest a possible solution, or at least in what direction to move?
Or, perhaps there is some other solution without forcing 0 to be shown, but simply aligning the characters vertically?
Any ideas are welcome. I really appreciate your help.
Update: A greate solution from HangarRash.
minimumFractionDigits = 1

How to get corresponding price of the smallest date chosen in date slicer in Power BI

I am quite new to working with DAX and Power BI so please don't judge. My problem seems (and might be) simple. Anyways, here we go:
I have a dataset that contains 3 colulmns: Date (date), Price (float), Performance (%)
Attribute descriptions:
Date and Price are constants that are pulled from an external data source. Performance is a variable of the price change over time in percent. It is the percentage change of the price of the current date to the first date in the time-series selection (Selected "from date" of date slicer visual).
I want to create a dynamic line chart that shows performance over time. Difficulty here is when I change the "from date" I want the performance to be variable. Meaning, the price of the chosen "from date" is the new base price and should be calculated accordingly.
Formula:
Date = t, price at date t = pt, performance at date t = pert
Date range:
1.1.2000 to 31.12.2010
Initial situation when "date from" in the date slicer visual = 1.1.2000:
t0 = 1.1.2000
pt0 = 5,00
pert0 = 0%
t5 = 6.1.2000
pt5 = 5,054
pert5 = (pt5-pt0)/pt0 = 1.08%
After changing date slicer so that "from date" is now 10.10.2009:
t0new = 10.10.2009
pt0new = 9,938
pert0new = 0%
t5new = 15.10.2009
pt5new = 9,832
pert5new = (pt5-pt0)/pt0 = -1,05%
As described, I want whatever is selected as starting point from the date slicer as the new base value for the performance calculation and the line chart should adjust accordingly.
I know how to do the dynamic line chart but I cannot figure out the measures and calculated columns I need to do so.
Any help is very much appreciated!
Cheers,
MLU
Calculate the benchmark as the price associated to the first date in
the period. SELECTEDVALUE assumes you have one price per Date,
otherwise use an aggregator (e.g. MIN, MAX, AVERAGE). I use ALLSELECTED so the Benchmark is affected only by Filter Context (slicers) and you can easily use it in visualizations that change the context.
Save our benchmark in a variable for later use
Divide each price by the benchmark. Here we need to apply an aggregator to the Price,
I used AVERAGE assuming you have only one Price per day, therefore, the result is the
price itself.
Here is the measure:
Price vs Dynamic Benchmark :=
VAR vbenchmark = CALCULATE(SELECTEDVALUE(Dataset[Price]),FILTER(ALL( Dataset[Date]), Dataset[Date] = CALCULATE(min(Dataset[Date])), ALLSELECTED(Dataset))
return
AVERAGE(Price) / vbenchmark

Rounding to two decimal places is not working

I am trying to reduce the decimal places of my number to two. Unfortunately is not possible. For this reason I added some of my code, maybe you will see the mistake...
Update [dbo].[company$Line] SET
Amount = ROUND((SELECT RAND(1) * Amount),2),
...
SELECT * FROM [dbo].[company$Line]
Amount in db which I want to change:
0.00000000000000000000
1914.65000000000010000000
376.81999999999999000000
289.23000000000002000000
Result I get after executing the code:
0.00000000000000000000
1366.28000000000000000000
268.89999999999998000000
206.38999999999999000000
Result I want to get (or something like this):
0.00000000000000000000 or 0.00
1366.30000000000000000000 or 1366.30
268.99000000000000000000 or 268.99
206.49000000000000000000 or 206.49
RAND() returns float.
According to data type precedence the result of multiplying decimal and float is float, try:
ROUND(CAST(RAND(1) as decimal(28,12)) * Amount, 2)
this should do the trick.

How to in calculate percentages in swift?

I am creating an expense ios application. I am using a Progress View to display how much income the user has left. So for example, if the user puts $100 income, and $30 expense, I'd like the bar to be 70% full. How do I calculate this?
I had originally put
let fractionalProgress = Float(expenseFloat!)/Float(balanceFloat!)
but this doesn't return the right value needed.
The formula in your question is correct if you want to know the percentage spent so far.
The percent remaining formula is "remaining balance" / "original balance".
And of course "remaining balance" is "original balance" - "total of expenses".
let fractionalProgress = (balanceFloat - expenseFloat) / balanceFloat
where balanceFloat is the original balance and expenseFloat is the total expenses.
In your example with a balanceFloat of 100 and an expenseFloat of 30 this gives (100 - 30) / 100 which is 0.7.
Of course you would multiple this result by 100 if you wish to show a percentage. Better yet, use a NumberFormatter setup with a .percent style.
We can calculate percentage in swift as below:
//Calucate percentage based on given values
public func calculatePercentage(value:Double,percentageVal:Double)->Double{
let val = value * percentageVal
return val / 100.0
}
We can use this function like:
let value = calculatePercentage(value: 500,percentageVal: 20)
print(value)
Output will be:
100

Rounding amount in javascript

I'm trying to round the amount in my form's total, tried several methods provided in different threads but none worked for me.
my form url is http://indushospital.org.pk/qurbani/
The amount shown in total is multiplied with 2.5% additional charges due to which its showing the amount like USD 186.63372, I want to show it like 186.64 Or simply 187.
The additional charges formula is mentioned below:
function getAmountPlusCharges(amount) {
// additionalCharges would result here '250' when '20000' is passed as amount.
var additionalCharges = (amount * 2.564) / 100;
return additionalCharges + amount;
Please help, I
'm not familiar with java functions at all
Thanks in advance.
You just have to use Math.round, then you can use to fixed to make sure its always 2 decimal places.
function getAmountPlusCharges(amount) {
return (Math.round(amount * 102.564 ) / 100).toFixed(2);
}