for (ctr = 0; ctr<cost.length; ctr++) { 
           cost[ctr] = price[ctr] * qty[ctr];
           totalCost = totalCost + cost[ctr];
           aveCost = totalCost / cost.length; }
            System.out.printf("Average Cost: %.2f\n\n", aveCost);

with this code the output will be, for example:

Average Cost: 15900.00

I want my output to look like this

Average Cost: 15,900.00

*the output number can change depends on what is inside the array. Is there an easy way? I look all over the internet and I all I see are those very long codes that takes up like 1 whole bond paper just to put the comma(,). I just want a very simple code which even a newbie like me can understand, I'm still not very good at programming.

Recommended Answers

All 2 Replies

you can create a small method, where you convert your float into a String, with the separator (the ,) on the correct spot. then you return that String and place that in your printf command

Does the printf method take formatting rules that will insert ,s into numeric output?
Look at the API doc and at the DecimalFormat class for example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.