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.

5 Years
Discussion Span
Last Post by NormR1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.