Dear All,
How to change number format of a field programmatically in Jasper?
please help
thanks
Dear All,
How to change number format of a field programmatically in Jasper?
please help
thanks
You can change the format using java.text.DecimalFormat class.
can you please give me an example ..? thanks in advance
You can have something like this in your Jasper template:
Code:java.text.NumberFormat.getInstance(java.util.Locale.US).format($F{FIELD_NAME})
Hi..
my case is I want to change number format of a textField in Jasper at run-time..
ie.
I want to change textField number format to ###,##0 (default is: ###,##0.00)
please help
Why would you like to change the Number format at run-time. It is a report and should have fix format of output.
I give user options for changing the numericformat when they want to run report..
the numeric format options are:
###,##0
###,##0.00
so, the numeric format of JTextField of the report should follow to chosen numeric format option
use tertiary format.
SYNTAX:
Code:condition ? expression (true part): expression (false part)
from java class.. how to change the JTextField pattern format ?
could you please give me an example?
Thanks in Advance
Nope. you will do this at iReport. You will need to create a parameter and a variable. At variable you create place your conditional expression or simply use the parameter to format the variable..
CONDITIONAL:
* ORCode:$P{param_input}.equals( new java.lang.String('someInput')) ? new java.text.DecimalFormat("#,##0").format($F{YOUR_FIELD}) : new java.text.DecimalFormat("#,##0.00").format($F{YOUR_FIELD})
USE THE PARAMETER INPUT.
USER INPUT should be in "#,##0" or "#,##0.00" only
Code:new java.text.DecimalFormat($P{param_input}).format($F{YOUR_FIELD})
I am also new to jasper, where should put the codes below:
new java.text.DecimalFormat($P{param_input}).format($F {YOUR_FIELD}) :confused:
From the Report Inspector click the variable that you created then from its properties you will see variable expression, type the code there.
Thanks,
what I have done are as follow:
I created a parameter named NUMBER_ FORMAT
and apply it on the field -> edit expression -> variables -> then put: new java.text.DecimalFormat($P{NUMBER_FORMAT}).format( $F{displaycost})
and with call it thru my java codes below:
Code:public void viewReport() {
try {
String jrxml = getJRXML();
if (cls1.isFileDirectoryExists(jrxml)) {
Map params = new HashMap();
params.put("NUMBER_FORMAT", "###,##0.0000"); // just for try
JasperDesign jasperDesign = JRXmlLoader.load(jrxml);
JRDesignQuery query = new JRDesignQuery();
query.setText(getSQL());
jasperDesign.setQuery(query);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Connection jdbcConn = connectMySQL();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jdbcConn);
JasperViewer.viewReport(jasperPrint, false);
} else {
cls1.msgBox(null, jrxml + " does not exists", Class1.MESSAGE_INFO);
}
} catch (Exception ex) {
cls1.msgBox(null, ex.toString() , Class1.MESSAGE_INFO);
}
}
I tried it but it gives me below exception:
net.sf.jasperreports.engine.fill.JRExpressionEvalE xception: Error evaluating expression:
Source.text: new.java.text.DecimalFormat($P{NUMBER_FORMAT}).for mat($F{displaycost})
Did you try to preview the report using just the iReport application?
strange.. it works if I try it using iReport application..
what I missed with my codes?
Hmm... I think we are having misunderstanding here....
Did you created a parameter called NUMBER_FORMAT and paste the code in its expression editor?
Lets check if you did these steps.
1. Create a parameter name it NUMBER_FORMAT. (as I can see you have done this).
2. Create a variable called New_DisplayCost.
*From report inspector(left side of your screen) right click VARIABLES(icon is like fx)>add variables(it will create new variable called variable1)>right click variable1>rename it to New_DisplayCost.
3. On the right side of your screen there is a properties window(if you dont see it goto Window>click Properties). From properties window you will see a VARIABLE Expression, paste the code there.
Thanks mine0926.. :)
it solved!
you saved my time.. :cool: