Results 1 to 13 of 13
Like Tree3Likes
  • 1 Post By asai
  • 1 Post By Tolls
  • 1 Post By Ksharp

Thread: Number format

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Number format

    Hi,

    I trying to use NumberFormat, but I dont find the correct alternativ.
    Anyone that could help?

    Here is my code:
    Java Code:
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    public class Result {
    	
    	 public static void main(String[] args) {
    		 
    		 NumberFormat nf = new DecimalFormat("### ###,##");
    		 
    		 double c = -66181.00;
             String sum = nf.format(c);
             sum = sum.replace("-", " ");
             System.out.println("Resultat: " +sum);
    		 
    	 }
    
    }
    This displays the amount like this: Resultat: 6 61 81
    What I want is this: Resultat: 66 181,00

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Number format

    The # symbol displays a digit when necessary, the 0 character always displays a digit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Number format

    Yes, thanks. But how can I get a space between 66 and 181?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Number format

    Quote Originally Posted by asai View Post
    Yes, thanks. But how can I get a space between 66 and 181?
    I'm not sure but if I were you I'd check the DecimalFormatSymbols class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Number format

    Great. Thanks.

    Heres my updated code:
    Java Code:
    import java.text.DecimalFormat;
    import java.text.DecimalFormatSymbols;
    import java.text.NumberFormat;
    
    public class Result {
    	
    	 public static void main(String[] args) {
    		 
    		 String format = "#,###,##0.00";
    		 DecimalFormatSymbols forSpace = new DecimalFormatSymbols();
    		 forSpace.setGroupingSeparator(' ');
    		 DecimalFormat formatter = new DecimalFormat(format, forSpace);
    		 
    		 double c = -66181.35;
             String sum = formatter.format(c);
             sum = sum.replace("-", " ");
             System.out.println("Resultat: " +sum);
    		 
    	 }
    
    }
    The result of this:
    66 181,35
    Fubarable likes this.

  6. #6
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Number format

    I am trying another format to.
    I have a text field where I write a number that is really a date.
    When I type this: 23011971 and change field, I would like the program to insert dots so the field looks like this:
    23.01.1971
    I have written this:
    Java Code:
    private void textDateFocusLost(java.awt.event.FocusEvent evt) {
            
            String formats = "##.##.####";
            DecimalFormat df = new DecimalFormat(formats);
            String dato1 = textDate.getText();
            String dato2 = df.format(dato1);
            textDate.setText(dato2);
    But I get this error:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "##.##.####"
    I guess this is because DecimalFormat cant be used this way.
    Any suggestions?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Number format

    Take a look at the SimpleDateFormat class
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Number format

    Those formatters are used for formatting number in fairly standardised ways. They also tend to use the locale to determine locale-specific separators.
    They are not intended for formatting your number in every possible way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Number format

    Quote Originally Posted by Norm View Post
    Take a look at the SimpleDateFormat class
    Uses this to convert the date from dd.MM.yyyy to java.sql.date for insert into mysql tables.
    Also for select from mysql and displaying the date in the same format. This works perfectly.
    As I mentioned in my last post, I would like to make a display of this format when I lose focus to the input field.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Number format

    Then use two formatters.
    One to go from String to Date (ddMMyyyy) then the other one goes from Date to String (dd.MM.yyyy).
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Number format

    Quote Originally Posted by Tolls View Post
    Then use two formatters.
    One to go from String to Date (ddMMyyyy) then the other one goes from Date to String (dd.MM.yyyy).
    Thats it! Works perfect with this code:
    Java Code:
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Result {
    	
    	 public static void main(String[] args) throws ParseException {
    		 
    		 SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
             String dato1 = "23011971";
             Date dato2;
             dato2 = df.parse(dato1);
             SimpleDateFormat dato3 = new SimpleDateFormat("dd.MM.yyyy");
             String dato4 = dato3.format(dato2);
             System.out.println(dato4);
     
    	 }
    
    }
    Thanks for the tip.

  12. #12
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Number format

    Or Handle it as a String.

    Java Code:
    
      public static void main(String[] args)  {
         StringBuilder sb=new StringBuilder("23011971");
         String new_date = sb.insert(2,'.').insert(5,'.').toString();
         System.out.println(new_date);
    Ksharp
    Tolls likes this.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Number format

    Ksharp's one makes more sense if this is going to be the same every time and you never actually need the date as a Date.
    Going from a string to a date and back again is going to be more "work" than simply plonking in a couple of '.' s.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Can't get the decimal format to format correctly...
    By Valerie1067 in forum New To Java
    Replies: 5
    Last Post: 03-25-2012, 04:15 AM
  2. Replies: 0
    Last Post: 02-09-2012, 09:25 AM
  3. Number Format Exception while parsing long
    By Aamir in forum Networking
    Replies: 2
    Last Post: 05-15-2011, 02:57 AM
  4. Replies: 7
    Last Post: 12-30-2010, 01:42 PM
  5. Replies: 16
    Last Post: 09-04-2010, 04:03 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
  •