Results 1 to 6 of 6
- 04-20-2011, 04:42 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
Quick question about numbers and decimals
Guys,
I completed a project for school but I am having a little problem formatting numbers.
I have the following line
The line and the code works but my problem is the result it yields has too many decimals. I tried the following line but it did not work:Java Code:JOptionPane.showMessageDialog(null, "Subtotal: " + subTotal);
I tried placing the %5.2f outside the parenthesis and it did not work.Java Code:JOptionPane.showMessageDialog(null, "Subtotal: %5.2f" , total);
Can someone help me fix this error?
- 04-20-2011, 04:53 AM #2
You need to use the String.format method.
Or you can use the NumberFormat/DecimalFormat classes.Java Code:JOptionPane.showMessageDialog(null, "Subtotal: " + String.format(......, subTotal));
- 04-20-2011, 05:14 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
I tried using the following String format but it gives me an error:
Any idea what I am doing wrong?Java Code:Shop.java:61: illegal start of expression JOptionPane.showMessageDialog(null, "Subtotal: " + String.format(%5.2f, subTotal)); ^ 1 error
- 04-20-2011, 05:17 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
String.format requires a String as the first argument. %5.2f is not a String, but "%5.2f" is.
- 04-20-2011, 05:19 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Java Code:JOptionPane.showMessageDialog(null, "Subtotal: " + String.format([color=red][b]"%5.2f"[/b][/color], subTotal));
"%5.2f" is the format string.
- 04-20-2011, 05:24 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 52
- Rep Power
- 0
Similar Threads
-
quick question
By biggerthanblue in forum New To JavaReplies: 2Last Post: 04-10-2011, 04:33 AM -
Really Quick question
By Ryan10 in forum New To JavaReplies: 63Last Post: 03-02-2011, 02:11 AM -
Really quick question
By shadycharacter in forum New To JavaReplies: 2Last Post: 04-22-2010, 10:06 PM -
Formatting numbers or decimals (around comma)
By Joris in forum Advanced JavaReplies: 1Last Post: 04-22-2010, 05:46 PM -
Quick question
By sAntA199 in forum New To JavaReplies: 2Last Post: 12-09-2009, 03:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks