count Float number with Integer in StringBuilder java

513 views Asked by At

I have products activity i want to count (Float) price * (Integer) quantity for each product than i need the total amount for all products... i tried by this code but the result it's weird orderItems is listview for selected products,

NItems is the quantity, itemprice is the price of each item

total_amount = new StringBuilder();

        for (int i = 0; i < orderItems.size(); i++) {

                    total_amount.append(Integer.parseInt(orderItems.get(i)
                            .getNItems()) * Float
                            .parseFloat(orderItems.get(i)
                                    .getItemPrice()));
        }
        if (total_amount.length()== 0)
            return false;
        else
            total_amount.deleteCharAt(total_amount.length()-1);

        Log.d("OrderPreview",total_amount.toString());
3

There are 3 answers

4
Elliott Frisch On

You aren't required to put everything on one line. I suggest breaking the statement into multiple parts. I suspect you wanted to use some form of formatted output and generally (for money) you would use NumberFormat.getCurrencyInstance() and then format your total with something like

NumberFormat nf = NumberFormat.getCurrencyInstance();
total_amount = new StringBuilder();
for (int i = 0; i < orderItems.size(); i++) {
    int count = Integer.parseInt(orderItems.get(i).getNItems());
    float price = Float.parseFloat(orderItems.get(i).getItemPrice());
    total_amount.append(nf.format(count * price)).append(" ");
}
Log.d("OrderPreview", total_amount.toString());

If you wanted a grand total that should probably be

float grandTotal = 0;
for (int i = 0; i < orderItems.size(); i++) {
    int count = Integer.parseInt(orderItems.get(i).getNItems());
    float price = Float.parseFloat(orderItems.get(i).getItemPrice());
    grandTotal += (count * price);
}
NumberFormat nf = NumberFormat.getCurrencyInstance();
total_amount = new StringBuilder();
total_amount.append(nf.format(grandTotal));
Log.d("OrderPreview", total_amount.toString());
0
nickle_nine On

Are you trying to display the total cost to purchase all the items? Such as outputting $50 if someone selects various items that together add up to $50? Elliot's answer is on the right track, but I wouldn't recommend using float or double to deal with money, due to issues with rounding and accuracy. Use BigDecimal instead.

0
Hussain Aali On

this is what i want, i found it when i read your answer, i want only the total amount ... before when i but for example (2(item(a))*2$ +4(item(b))*1.2$) the result is com 4.04$ but now is correct thanks every body

total = new StringBuilder();
                float total_amount = 0;

                for (int i = 0; i < orderItems.size(); i++) {

                            total_amount=total_amount+(Integer.parseInt(orderItems.get(i)
                                    .getNItems()) * Float
                                    .parseFloat(orderItems.get(i)
                                            .getItemPrice()));
                }
                total.append(total_amount);