CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Posts
    17

    Formatting Decimals

    I have this program here that reads in strings and doubles. I need to return the doubles in 2 decimal places. What do I need to add to the Reader file to achieve this? I know it's in java.text, but I'm not sure of the right syntax. The quantity and total should not have decimal places. Should those be read in as integers and if so, how do I do that? Thanks!



    import java.io.*;
    import java.text.*;

    public class Product
    {

    class Reader
    {
    BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
    public String readString()
    {
    String r=null;
    while(r==null)
    try
    {
    r=reader.readLine();
    }catch(Exception e){
    r=null;
    System.out.print("again:");
    }
    return r;
    }

    public double readDouble()
    {
    Double r=null;
    while(r==null)
    try
    {
    r = new Double(readString());
    }catch(Exception e){
    System.out.println("Exception:"+e.toString());
    r=null;
    System.out.print("again:");
    }
    if(r!=null)
    return r.doubleValue();
    else
    return -1;
    }
    }

    Reader reader = new Reader();
    public static void main(String[]args)
    {
    Product app = new Product();
    app.go();
    }

    public void go()
    {
    Productinfo productinfo = new Productinfo();
    productinfo.getData();
    productinfo.displayData();
    productinfo.displayTotal();
    try{System.in.read();}catch(Exception e){};
    }

    public class Productinfo
    {
    private String name, idnumber;
    private double price, total, quantity;

    public void getData()
    {
    System.out.print("Name:");
    name=reader.readString();

    System.out.print("ID Number:");
    idnumber=reader.readString();

    System.out.print("Price:");
    price=reader.readDouble();

    System.out.print("Quanity:");
    quantity=reader.readDouble();
    }
    public void displayData()
    {
    System.out.println("The product is " + name);
    System.out.println("The ID Number is " + idnumber);
    System.out.println("The price is " + price);
    System.out.println("The quantity is " + quantity);
    }
    public void displayTotal()
    {
    double total=(price * quantity);
    System.out.println("The total value is " + total);
    }
    }
    }




  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Formatting Decimals


    > I need to return the doubles in 2 decimal places

    Here is a sample program..



    import java.text.*;

    public class NumFormat{

    public static void main( String[] str ){
    double d = 123.45677;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2); // This will set the max no. of fractions.
    String str2 = nf.format( d );
    System.out.println( new Double(str2).doubleValue() );
    }
    }





    > Should those be read in as integers

    Dont read the data as an int or double from the console. You will get a junk value.
    The reason is , If you enter a data in the console window , each and every piece of the
    data will be interpreted as char. If you look inside readInt() of DataInputStream() ( There
    is not function called readInt() in BufferedReader )


    public final int readInt() throws IOException {
    InputStream in = this.in;
    int ch1 = in.read();
    int ch2 = in.read();
    int ch3 = in.read();
    int ch4 = in.read();
    if ((ch1 | ch2 | ch3 | ch4) < 0)
    throw new EOFException();
    return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    }




    it reads 4 bytes from the console. Say for example , you have entered 12345 in
    the console. This function reads first four bytes , that means , 1234 from the console
    and trying to create an integer. This function will be very helpful when you try
    to read an integer from a file.

    Check my previous post to read an int from the console.

    Poochi...


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured