Results 1 to 13 of 13
Thread: Number format
- 06-19-2012, 03:35 PM #1
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Number format
Hi,
I trying to use NumberFormat, but I dont find the correct alternativ.
Anyone that could help?
Here is my code:
This displays the amount like this: Resultat: 6 61 81Java 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); } }
What I want is this: Resultat: 66 181,00
- 06-19-2012, 03:38 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
Re: Number format
The # symbol displays a digit when necessary, the 0 character always displays a digit.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-19-2012, 04:25 PM #3
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Number format
Yes, thanks. But how can I get a space between 66 and 181?
- 06-19-2012, 04:29 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
- 06-19-2012, 10:14 PM #5
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Number format
Great. Thanks.
Heres my updated code:
The result of this: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); } }
66 181,35
- 06-20-2012, 03:31 PM #6
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
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:
But I get this error: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);I guess this is because DecimalFormat cant be used this way.Java Code:Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "##.##.####"
Any suggestions?
- 06-20-2012, 03:45 PM #7
Re: Number format
Take a look at the SimpleDateFormat class
If you don't understand my response, don't ignore it, ask a question.
- 06-20-2012, 03:50 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-20-2012, 04:06 PM #9
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Number format
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.
- 06-20-2012, 04:44 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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).Please do not ask for code as refusal often offends.
- 06-20-2012, 07:36 PM #11
Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 96
- Rep Power
- 0
Re: Number format
Thats it! Works perfect with this code:
Thanks for the tip.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); } }
- 06-21-2012, 07:37 AM #12
Banned
- Join Date
- Jun 2012
- Location
- Beijing,China
- Posts
- 34
- Rep Power
- 0
Re: Number format
Or Handle it as a String.
KsharpJava 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);
- 06-21-2012, 10:24 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
Similar Threads
-
Can't get the decimal format to format correctly...
By Valerie1067 in forum New To JavaReplies: 5Last Post: 03-25-2012, 04:15 AM -
Auto Detecting Email Message File Format & Support for EMLX Format
By sherazam in forum Java SoftwareReplies: 0Last Post: 02-09-2012, 09:25 AM -
Number Format Exception while parsing long
By Aamir in forum NetworkingReplies: 2Last Post: 05-15-2011, 02:57 AM -
catch the number format exception, where to place try
By aborgeld in forum New To JavaReplies: 7Last Post: 12-30-2010, 01:42 PM -
How to change number format of a field programmatically in Jasper?
By newbiejava in forum New To JavaReplies: 16Last Post: 09-04-2010, 04:03 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks