Results 1 to 3 of 3

Thread: Format Numbers

  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Format Numbers

    Hello

    In the following code, I want to do like this

    if the number 24.543355
    I want the programe gives me 24.5$

    I've tried many times to fix the istake but I couldn't

    Java Code:
    import java.text.NumberFormat;
    
    public class CuurencyExample {
    	public static void main(String[] args){
    		double price = 24.99;
    		double tax = price * 0.15;
    		double total = price + tax;
    		
    		NumberFormat formatter = NumberFormat.getCurrencyInstance();
    		NumberFormat formatter1= NumberFormat.getNumberInstance();
    		
    		formatter1.setMaximumFractionDigits(1);
    		
    		
    		
    		System.out.println("Price:\t" +
    				formatter.format(price)+
    				formatter1.format(price));
    				System.out.println("Tax:\t" +
    				formatter.format(tax)+
    				formatter1.format(tax));
    				System.out.println("Total:\t" +
    				formatter.format(total)+
    				formatter1.format(total));
    				
    	}
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    'if the number 24.543355
    I want the programe gives me 24.5$' <---did you mean $24.54

    if so then you just need to adjust slightly:

    Java Code:
    import java.text.NumberFormat;
    
    
    public class CuurencyExample{
    	public static void main(String[] args){
    		double price = 24.99;
    		double tax = price * 0.15;
    		double total = price + tax;
    	
    		NumberFormat formatter = NumberFormat.getCurrencyInstance();
    		
    		
    		
    		System.out.println("Price:\t" +
    				formatter.format(price));
    				
    				System.out.println("Tax:\t" +
    				formatter.format(tax));
    				
    				System.out.println("Total:\t" +
    				formatter.format(total));
    				
    				
    	}
    	
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Or you can use the Formatter class.

    Java Code:
    System.out.printf("%.1f$", 12.345 * 2); // 24.7$

Similar Threads

  1. Format Specifiers
    By Cass29 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 04:49 PM
  2. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 11:21 AM
  3. Format
    By 2ndis1stplaceloser in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 07:38 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 04:37 AM

Posting Permissions

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