Results 1 to 2 of 2
  1. #1
    dandapice is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Trying to convert output to a 2 decimal place value

    Hello Again,
    Well my program (code is below) is working with one exception: I need the "Restock Price" to display in a dollar and cents format (e.g. 150.45, or 2,150.99) I guess that would be called a 2 decimal place format? Any help would be greatly appreciated.
    Java Code:
    package inventory;
    import java.util.ArrayList;
    import java.util.Arrays;
    /**
     * Inventory Class
     * @author Dan
     * Modified 9/27/2012
     */
    public class Inventory {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            /* Create new variable for inventory calculation */
            double Total = 0.0;
            
            /* Create new ArrayList */
            ArrayList<Book> BookList;
            BookList = new ArrayList<>();
            ArrayList<TrainingBook> TrainList;
            TrainList = new ArrayList<>();
            
            /* Add elements to ArrayList */
            BookList.add(new Book("001a", "Java 101", 50, 19.99) );
            BookList.add(new Book("001b", "C# 101\t", 50, 19.95) );
            BookList.add(new Book("001c", "Python 101", 25, 25.99) );
            TrainList.add(new TrainingBook("001a", "Java 101", 50, 19.99, "Training"));
            TrainList.add(new TrainingBook("001b", "C# 101\t", 50, 19.95, "Training"));
            TrainList.add(new TrainingBook("001c", "Python 101", 25, 25.99, "Training"));
            
            /* Print out data from elements in ArrayList */
            System.out.println("Item#\tBook Name\tIn Stock\tPrice Each\tItem Class\tRestock Price*");
            System.out.println(TrainList.get(0) );        
            System.out.println(TrainList.get(1) );
            System.out.println(TrainList.get(2) );
            
            /* Calculate total inventory value */
            for (Book List : BookList)
            {
                Total += List.getTotal();
            }
            /* Display total inventory value */
            System.out.printf("\nThe total value of the inventory is: $%,.2f\n", Total);
            System.out.println();
            System.out.println("* Restock Price reflects a 5 percent restocking fee for entire stock of each specific item.");
            System.out.println();
        } /* End main */
    } /* End class */
    Java Code:
    package inventory;
    
    /**
     * Book Class
     * @author Dan
     * Modified 9/27/2012
     */
    public class Book implements Comparable{
        /* Define variables */
        
        protected String ItemNumber;
        protected String ItemName;
        protected Integer InStock;
        protected double PriceEach;
        
        /* Parameterized Constructor */
        public Book(String ItemNumber, String ItemName, Integer InStock, double PriceEach)
        {
            this.ItemNumber = ItemNumber;
            this.ItemName = ItemName;
            this.InStock = InStock;
            this.PriceEach = PriceEach;
    
        }
        
        /* Calculate value of stock */
        public double getTotal()
        {
            return this.PriceEach * this.InStock;
        }
        /* Define Set and Get functions */
        public void setItemNumber(String itemNumber)
        {
            ItemNumber = ItemNumber;
        }
        public String getItemNumber()
        {
            return ItemNumber;
        }
        public void setItemName(String itemName)
        {
            ItemName = ItemName;
        }
        public String getItemName()
        {
            return ItemName;
        }
        public void setInStock(Integer inStock)
        {
            InStock = InStock;
        }
        public Integer getInStock()
        {
            return InStock;
        }
        public void setPriceEach(double priceEach)
        {
            PriceEach = PriceEach;
        }
        public double getPriceEach()
        {
            return PriceEach;
        }
        
        /* Sort array by name? */
        @Override
        public int compareTo(Object o)
        {
            Book inputParameter = (Book)o;
            return ItemName.compareTo(inputParameter.ItemName);
        }
        /* Override otString to display array data */
        @Override
        public String toString()
        {
            return String.format(ItemNumber + "\t" + ItemName + "\t" + InStock + "\t\t" + PriceEach);
        }
    } /* End class */
    Java Code:
    package inventory;
    /**
     * Subclass of Book
     * @author Dan
     * Created 9/28/2012
     */
    public class TrainingBook extends Book{
        /* Declare new variables */
         String ItemClass;
         
        /* Inheret conductor from Book */
        public TrainingBook(String itemNumber, String itemName, Integer inStock, double priceEach, String itemClass) 
        {
            super(itemNumber, itemName, inStock, priceEach); 
            this.ItemClass = itemClass;
        }
        
        /* New Method for Item Class */
        public void setItemClass(String ItemClass)
        {
            ItemClass = ItemClass;
        }
        public String getItemClass()
        {
            return ItemClass;
        }
    
        /* Override and modify Calculation from Book class*/
        @Override
        public double getTotal()
        {
            return (this.PriceEach * this.InStock) * 1.05;
        }
    
        /* Override toString to format output */
        @Override
        public String toString()
        {
            return String.format(ItemNumber + "\t" + ItemName + "\t" + InStock + "\t\t" + PriceEach + "\t\t" + ItemClass + "\t" + getTotal() );
        }
    }
    OUTPUT:
    Java Code:
    run:
    Item#	Book Name	In Stock	Price Each	Item Class	Restock Price*
    001a	Java 101	50		19.99		Training	1049.475
    001b	C# 101		50		19.95		Training	1047.375
    001c	Python 101	25		25.99		Training	682.2375000000001
    
    The total value of the inventory is: $2,646.75
    
    * Restock Price reflects a 5 percent restocking fee for entire stock of each specific item.
    
    BUILD SUCCESSFUL (total time: 0 seconds)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trying to convert output to a 2 decimal place value

    You're already using String#format(...). Apply appropriate formatting codes -- see the API for the method, which has a link to the API where you can find all available formatting codes.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. One Decimal Place
    By Akirien in forum New To Java
    Replies: 1
    Last Post: 08-21-2012, 11:16 AM
  2. Output is off by very small decimal place
    By jester-race in forum New To Java
    Replies: 3
    Last Post: 02-14-2012, 10:08 AM
  3. convert to decimal
    By rjagan in forum New To Java
    Replies: 6
    Last Post: 06-21-2011, 01:34 PM
  4. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  5. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •