Format field from SQL Server to currency?
Here is my problem in a nutshell....
I have a JSP that is connecting to a database on SQL Server 7.0..one of the fields is a currency...in SQL the data type is money...in the output on the JSP, I need the field to read with a $ and 1000 seperator...
I can not for the life of me find out how to do this:mad: ...
Please, any help or workarounds will be appreciated!!
Thank you.
Re: Format field from SQL Server to currency?
Hey Dilinger4,
I appreciate your comments...I am hoping that you are reading this today, because I could use some of your expertise in solving this problem of mine....
You see I am having no problem incorporating java, javascript and HTML into dynamic JSP's....I am fairly new to this and the proper structure for such pages...The pages I am creating are used within an INTRANET structure that is utilized acrross the world for a major Financial Institution...
What I need to know is how I can format the money value that I am retrieving from the SQL Server into a currency format...I have attempted several things such as javascript functions, but I can not get the eventHandler to be called while the values are being retrieved in the loop.
I have also imported the java.text and java.util packages in order to use the NumberFormat...but to no success....
I am very hopeful that either yourself or others could help to resolve this imperative issue of mine...
Thank you
Re: Format field from SQL Server to currency?
You need to pre-process the values. Currently you have them as strings, which is pretty bad for processing.
prodRow[t]
You first need to parse them into integers.
Int.parseInt(prodRow[t])
Or longs, if the values might be larger than 4 billions.
Long.parseLong(prodRow[t])
Or BigDecimal, if you have places after the comma.
new java.math.BigDecimal(prodRow[t])
Then you have a proper value and can format it using the java.text package.
java.text.NumberFormat.getCurrencyInstance(Locale.US).format(new java.math.BigDecimal(prodRow[t]))
Feel free to introduce temporary variables at will.
Re: Format field from SQL Server to currency?
Hello CornedBee,
Thank you very much for your suggestion...
I just have one followup question, that I am hoping you could assist me with...
When should the parsing occur?? What I mean is that I am retrieving the data by way of a loop...so I am not sure where in the code I should place the parsing to Integers??
Here is the snippet of the loop...my previous replies in this thread show the complete code:
Code:
String prodRow[];
String bg="#003366";
while ((prodRow=q2.s_next())!=null) {
for (int t=0; t<q2.getNumCols(); t++) {
if (t==0) {
out.print("<td align='center' bgcolor="+bg+"><font size=2 color='white'>"+prodRow[t]+"</font></td>");
} else if (t==1) {
out.print("<td align='center' bgcolor="+bg+"><font size=2 color='white'>"+prodRow[t]+"</font></td>");
} else if (t==2) {
out.print("<td align='center' bgcolor="+bg+"><font size=2 color='white'>"+prodRow[t]+"</font></td>");
}
}
out.println("</tr>");
if (bg.equals("#003366")) bg="#000000"; else bg="#003366";
}
I hope that you can assist me with this...thank you!
Re: Format field from SQL Server to currency?
Inside the loop. (Mind you, the inner loop you have is rather useless, but that's a different topic.) Just before the ifs, you create a local variable that holds the final value and calculate it from the database cell. Then you can use it instead of the row inside the HTML.
Re: Format field from SQL Server to currency?
Ok, I probaly should have stated that I am fairly new to JAVA....so I apologize for my ignorant questions...
Here is what I have, but I am receiving errors from:
Code:
while ((prodRow=q2.s_next())!=null) {
for (int t=0; t<q2.getNumCols(); t++) {
Int nwProdrow[] = Int.parseInt(prodRow[t]);
if (t==0) {
out.print("<td align='center' bgcolor="+bg+"><font size=2 color='white'>"+nwProdrow[t]+"</font></td>");
} else if (t==1) {
out.print("<td align='center' bgcolor="+bg+"><font size=2 color='white'>"+nwProdrow[t]+"</font></td>");
} else if (t==2) {
System.out.println(nf.format("+nwProdrow[t]+"));
}
}
out.println("</tr>");
if (bg.equals("#003366")) bg="#000000"; else bg="#003366";
}
The error is as follows from my Web Server 6.0 Error Log:
JasperException: Unable to compile class for JSPC:\iPlanet\Servers\https-het7\config\..\ClassCache\_jsps\_sams\_sams_rep_result_jsp.java:119: Class _jsps._sams.Int not found. Int nwProdrow[] = Int.parseInt(prodRow[t]); ^
Do you know what I am doing wrong?
Also if you notice in the output to the table in HTML, the third column represented under the else if (t==2) statement is where the dollar amount is to show...while the other 2 columns are using the out.print, I am not sure how to output the format for the currency....
Thank you so much for your time and consideration!!
Re: Format field from SQL Server to currency?
First things first.
Int nwProdrow[] = Int.parseInt(prodRow[t]);
This should be
int curprod = Integer.parseInt(prodRow[t]);
Then replace the nwProdrow[t] with curprod.
That should get rid of the error, though it may not yet show what you want.
Re: Format field from SQL Server to currency?
OK....NO Errors!!
But as you probably assessed, no results either....
So where am i going wrong???
I mean that I was able to show the results prior to this...and so that you may understand the output data that I am trying to show...
The first column of data should be string, the 2nd and 3rd is integer...the last column which is why I created this thread is the currency field....
Also the data for the last 2 columns are set up in the SQL server as integer and money respectively....
So the output should have 1st column string followed by the integer then currency...I am assuming that the problem lies in the output to the table???
You have helped me greatly so far, maybe you have a moment to see if I can get this to work with what I have???
Thank you.