Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java question, how to show commas

  • 08-02-2009 8: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,639 ✭✭✭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,329 ✭✭✭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