Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java question, how to show commas

  • 08-02-2009 09:51AM
    #1
    Closed Accounts Posts: 4


     public static void main(String [] args)
     {
     
      double price = 1.50;
     
         while (price <= 3.25)
         {
            System.out.print(Math.round((18000/5) * price) + "   ");
            price = price + .25;
         } 
     
     }// end Main()
    

    All i want is for the numbers to show comma's in them


    for ex: it shows this now
    5400   6300   7200   8100   9000   9900   10800   11700
    

    I want it to show
    5,400   6,300   7,200   8,100   9,000   9,900   10,800   11,700
    


    I tried to use this as the printf line but it didnt work
    System.out.printf("%,f\n", Math.round((18000/5) * price) + "   ");
    

    Any help is much appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 2,791 ✭✭✭LowOdour


    Couldnt you build you a string with the comma in it?
    Long time since I wrote any java but il try help.

    Will your out put always be a round number to the nearest 1000....shown from you example?
    (5,400 6,300 7,200 etc).
    If so, you could build up an output by using a substing (or java equivilant) as you know the comma will come before the last 3 digits.
    Im abit hungover but that may give you a start unless you can provide some more details on what kind of output will be displayed


  • Registered Users, Registered Users 2 Posts: 25 DeJaMo


    There should be something in the NumberFormatter class that can do this. Have a look in the API.


  • Closed Accounts Posts: 4 machan188


    still stuck
    /arghh!
    i tried using this site for help
    i tried this below but it didnt work
    import java.text.*;
     
    public static void main(String [] args)
    {
      double price = 1.50;
     
      while (price <= 3.25)
      {
         DecimalFormat df = new DecimalFormat("#####0.00");
      [COLOR=yellowgreen]// System.out.println(df.format(d)); -- this was the example given[/COLOR]
         System.out.print(df.format(Math.round(18000/5 * price) + "    "));
         price = price + .25;
      } 
    }// end Main()
    


    and i also tried this from that same link above
    import java.util.Locale;
     
    public static void main(String [] args)
    {
      double price = 1.50;
     
      while (price <= 3.25)
      {   
       [COLOR=yellowgreen]//System.out.format("%,8d%n", n);   -->  " 461,012"  - example given[/COLOR]
         System.out.printf("%,8d%n", (Math.round(18000/5 * price) + "    "));
         price = price + .25;
      } 
    }// end Main()
    

    Anyhow i tried both of those and they didnt work. so i'm doing something wrong and now im' at a loss.


  • Registered Users, Registered Users 2 Posts: 3,335 ✭✭✭padraig_f


    this what you want?
    System.out.println( NumberFormat.getInstance().format(1234567890) );
    

    from http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    Sorry misread that
    padraig has it


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    Your mistake is

    System.out.print(df.format(Math.round(18000/5 * price) + " "));

    Should be
    System.out.print(df.format(Math.round(18000/5 * price)) + " ");

    Also, if you want to use commas your decimal format should be:

    DecimalFormat df = new DecimalFormat("#,###,##0")


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html

    All the currency formatting you will ever need.


Advertisement