How do I separate numbers with a comma , in a result set?
I am running the following code that is being placed in a table. The dollar amounts, I would like to be separated like this $10,000.00 instead it is just showing as 10000.
Can anyone please suggest how to get a comma (and possibly a dollar sign) in there?
String sql = "SELECT [premium].[amount] AS premiumAmount FROM
premium;";
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:laGuardia","","");
Statement stmt =con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
%>
<table border = "1" align = "center">
<tr>
<th> Premium Amount</th>
</tr>
<%
while (rs.next())
{
out.print("<td>"+ rs.getInt("premiumAmount") +"</td>");
out.print("</tr>");
}
%>
</table>
<% rs.close();%>
# 1 Re: How do I separate numbers with a comma , in a result set?
NumberFormat formatter = NumberFormat.getCurrencyInstance();
System.out.println(formatter.format(1000));
using java.text.NumberFormat class..
this will produce a result
depend on your system( i guess system language ).
if you want different output, look at the getCurrencyInstance(Locale l) version ..
mr1yh1 at 2007-11-11 22:39:42 >

# 2 Re: How do I separate numbers with a comma , in a result set?
Thanks for the tip. However, I am now getting an error message as follows:
Class org.apache.jsp.NumberFormat not found.
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
The code looks like this:
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
int rc = 0;
while (rs.next())
{
rc = rs.getInt("rc");
out.println("<td>");
out.println(formatter.format(rc));
out.println("</td>");
}
And I remembered to import two classes in the import statement as:
import ="java.text.NumberFormat.*" import="java.util.Locale.*"
Any more suggestions on how to get JSP to recognise this NumberFormat class?
# 3 Re: How do I separate numbers with a comma , in a result set?
Hey,
I just got it to work! All I had to do was place the entire class name before the NumberFormat.
Now how do I use a percentage?
That same code has an out.println with integers that I want to place with a percentage sign.
Does anyone know what method I have to use within this class?
# 4 Re: How do I separate numbers with a comma , in a result set?
formatter = NumberFormat.getPercentInstance()
when you parse ,
result is similar this : 0.5 --> 50%
mr1yh1 at 2007-11-11 22:42:50 >

# 6 Re: How do I separate numbers with a comma , in a result set?
Even though I am seeing the percentage sign, I am having a problem with the way the data is being displayed. In the database, the data is stored as .80 - meaning 80%. However on the browser it is displaying as 0%. If I change the data in the database to 80 , on the browser it is displaying as 8000%. If I change the data to 8% on the browser it is shown as 800%.
Can anyone please suggest how to get this thing to show as 80%. My code goes like this:
java.text.NumberFormat percent = java.text.NumberFormat.getPercentInstance();
int rcPercent = 0;
while (resultset.next())
{
rcPercent = rs.getInt("rcPercent");
out.println("<td>");
out.println(percent.format(rcPercent));
out.println("</td>");
}