How to change the total amount? - flutter

I’m building a shop app and i have many products, I want to put an offer that when the user put an order for 2 pieces of the same product he get the third one for free, so he put 3 in the cart but the total amount is for 2, the same if he ordered 3,6,9,.....
this var is for the total price, the counter is the quantity
double usedPrice = getCurrentProduct.isOnSale
? getCurrentProduct.salePrice
: double.parse(getCurrentProduct.price);
double totalPrice = usedPrice * int.parse(counter.toString());

After calculating totalPrice you can just update the quantity by
counter = counter + (counter / 2).floor() ;

Related

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

Creating a BMI Ideal Weight calculator (Running into some errors)

I am creating a BMI Ideal Weight calculator where the user is prompted to input data or there information into the textboxes, such as their name, height, weight (the weight can be in metric aka kgs or in imperial aka pounds) and the height can either be in metres or in feet. I also am using a radiobutton, two different ones to distinguish between genders (male or female) because males and females have different BMIs. The information and formulas I'm supposed to refer to is in the photo below, but my problem is so far when I am coding it and I'm just testing out for the male radio button because I completed it, nothing is outputting into my jLabel aka lblOutput...
Here is a quick rundown of what I want:
1)User is prompted to click on gender (radiobutton)
2)User is then needed to fill in the information
3)Supposed to output BMI and telling the person if it's normal, underweight, etc.
Here is the code (So far what I have):
String name = txt1.getText();
double Metric = Double.parseDouble(txt2.getText());
double Imperial = Double.parseDouble(txt2.getText());
double metres = Double.parseDouble(txt3.getText());
double inches = Double.parseDouble(txt4.getText());
double kgs = Double.parseDouble(txt5.getText());
double pounds = Double.parseDouble(txt5.getText());
double weight;
double weight2;
int m = 1;
int i =2;
int me =3;
int in=4;
if (rdbMale.isSelected()) {
if (Metric==m) {
//inches = System.null(); //need to learn how to make it null or dissapear if user inputs for metric instead of imperial
weight = metres/Math.pow(kgs, 2);
lblOutput.setText("Your name is " + name + " and your ideal weight in kgs is " + String.format("%.2f", weight));
}
} else if (Imperial==i) {
weight2 = inches/Math.pow(pounds, 2)*703;
lblOutput.setText("Your name is " + name + " and your ideal weight in pounds is " + String.format("%.2f", weight2));
}
// some codes are unneccessary like the int me, or the int in i used them so i can distinguish between which one the user wants to chose from either 3 for metres or 4 for inches, pls ignore that
//I basically just want this to work and I don't know how to get it to work

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);
}

Getting the total number of records in PagedList

The datagrid that I use on the client is based on SQL row number; it also requires a total number of pages for its paging. I also use the PagedList on the server.
SQL Profiler shows that the PagedList makes 2 db calls - the first to get the total number of records and the second to get the current page. The thing is that I can't find a way to extract that total number of records from the PagedList. Therefore, currently I have to make an extra call to get that total which creates 3 calls in total for each request, 2 of which are absolutely identical. I understand that I probably won't be able to rid of the call to get the totals but I hate to call it twice. Here is an extract from my code, I'd really appreciate any help in this:
var t = from c in myDb.MyTypes.Filter<MyType>(filterXml) select c;
response.Total = t.Count(); // my first call to get the total
double d = uiRowNumber / uiRecordsPerPage;
int page = (int)Math.Ceiling(d) + 1;
var q = from c in myDb.MyTypes.Filter<MyType>(filterXml).OrderBy(someOrderString)
select new ReturnType
{
Something = c.Something
};
response.Items = q.ToPagedList(page, uiRecordsPerPage);
PagedList has a .TotalItemCount property which reflects the total number of records in the set (not the number in a particular page). Thus response.Items.TotalItemCount should do the trick.

drools - get index of item in collection

I have a restaurant app and have a rule where a 20% discount needs to be offered for every 2nd ice-cream.
So,
If bill has 2 icecreams, 20% discount on the 2nd icecream
If bill has 3 icecreams, still 20% discount on the 2nd icecream
If bill has 4 icecreams, 20% discount on the 2nd and 4th icecreams
I have a collection called $bill.items which contains each individual item in the bill.
How can I write this rule in Drools given that there seems to be no way to access the index of an element in a collection.
Just collect them up and apply the discounts on the right-hand-side:
rule "Discount multiple ice creams"
when
$bill : Bill()
$iceCreams : ArrayList( size > 1 ) from $bill.items
then
for (int i = 0; i < $iceCreams.size(); i++) {
if (i % 2 == 0) {
// Apply a discount
}
}
end
Or if each bill item is available in working memory, the following can be used on the LHS to collect them:
$iceCreams : ArrayList( size > 1 )
from collect( BillItem(type == "Ice Cream") )
You may need to re-sort the list you have collected, based on each item's index within the bill.
Although, does the order of the items on a single bill really matter? The order in which items are entered on a bill is a rather unusual basis for a discount. As a customer buying 2 ice creams of differing price, I would ask for the cheapest item first because I will get a bigger discount on the second ice cream added to my bill. Hence why such discounts are usually applied to the N cheapest items. i.e. If 4 ice creams are purchased, then the 2 cheapest are discounted. Also, are ice creams different prices? If each ice cream is the same price, then all you really need to know is how many need to be discounted.