Results 1 to 5 of 5

Thread: displaying a float to 2 decimal places {Resolved} thanks

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    displaying a float to 2 decimal places {Resolved} thanks

    Hello

    I am using a float to give a anwer in dollars, but how can l display the currency to 2 decimal places.

    e.g.

    416.66

    Thanks in advance
    Last edited by steve_rm; Nov 9th, 2003 at 12:53 AM.
    steve

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I guess you could do it like this.
    Code:
    import java.text.*;
    import java.text.NumberFormat.Field;
    
    public class F {
       public static void main(String[] args) {
    		
    	double d = 3.333333333333;
    	StringBuffer sb = new StringBuffer(); 
    	DecimalFormat df = new DecimalFormat();
    	df.setMaximumFractionDigits(2);
    	df.format(d, sb, new FieldPosition(Field.DECIMAL_SEPARATOR));
    
    	System.out.println(d);
    	System.out.println(sb);
      }
    }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Since you are working with currency i guess this would be a better snippet.
    Code:
    import java.text.*;
    import java.util.Locale; 
    import java.text.NumberFormat;
    import java.text.NumberFormat.Field;  
    
    public class F {
     public static void main(String[] args) {
           
            NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
            String field1 = args[0];
            String field2 = args[1]; 
            double d1 = formatPlaces(field1);
            double d2 = formatPlaces(field2);
            double temp = d1 + d2; 
            System.out.println(temp);
            String s = nf.format(temp);
            System.out.println(s);
    	
    		
    }
     public static double formatPlaces(String field){
         
           StringBuffer sb = new StringBuffer(); 
           DecimalFormat df = new DecimalFormat();
           df.setMaximumFractionDigits(2);
           df.format(Double.parseDouble(field), sb, new FieldPosition(Field.DECIMAL_SEPARATOR));
           return Double.parseDouble(sb.toString());
          
     }
    }

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    sorry forgot the error handling.
    Code:
    import java.text.*;
    import java.util.Locale; 
    import java.text.NumberFormat;
    import java.text.NumberFormat.Field;  
    
    public class F {
     public static void main(String[] args) {
          try{
            NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
            String field1 = args[0];
            String field2 = args[1];
           
            double d1 = formatPlaces(field1);
            double d2 = formatPlaces(field2);
            double temp = d1 + d2; 
            String s = nf.format(temp);
            System.out.println(s);
    
         }catch(Exception e){System.err.println(e);}
    		
    }
     public static double formatPlaces(String field) throws NumberFormatException{
         
           StringBuffer sb = new StringBuffer(); 
           DecimalFormat df = new DecimalFormat();
           df.setMaximumFractionDigits(2);
           df.format(Double.parseDouble(field), sb, new FieldPosition(Field.DECIMAL_SEPARATOR));
           return Double.parseDouble(sb.toString());
          
     }
    }

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved, thanks for you help

    Thanks for you help

    Steve
    steve

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