dojo1234 Posted March 13, 2007 Share Posted March 13, 2007 Hello, I have a question:I want to define an individual 1000 seperator for number formating. f.ex. I retrieve from the database an integer like 12345 and I want the output to be like 12''345.Can anybody help me? Thank you Link to comment Share on other sites More sharing options...
dojo1234 Posted March 15, 2007 Author Share Posted March 15, 2007 or can someone explain how to use DecimalFormater Post edited by: dojo1234, at: 2007/03/15 08:48 Link to comment Share on other sites More sharing options...
schaibaa Posted March 15, 2007 Share Posted March 15, 2007 To my knowledge iReport only supports the built in formatting. That said, there are several ways to approach the problem. Anything you write in Java on the RHS of an expression can be written in the expression field of iReport. If you know the max digits of your field, you can make a nested set of ternary "if"'s to solve it - albeit ugly, it's only presentation logic. The easier way might be to manipulate the data before it reaches iReport. Not sure what you have access to. As far as the ternary solution, check the length - if it's length is >3, then create a substring and add the " to it. Anyway, there's no quick and easy solution in iReport (that I'm aware of) Link to comment Share on other sites More sharing options...
jmurray Posted March 16, 2007 Share Posted March 16, 2007 You could use a bit of code in a scriptlet. I don't know java, so you'll have to forgive me if the following code makes your eyes bleed: Code:public String formatDecimal(Integer myIntegerValue, Integer myDecimalPlaces, String myDecimalSeparator, Boolean myLeadingZero) throws JRScriptletException{String decimalSeparator = (java.lang.String)( myDecimalSeparator == "" ? "." : myDecimalSeparator );Integer decimalPlaces = (java.lang.Integer)( myDecimalPlaces.intValue());String integerValue = (java.lang.String)(myIntegerValue.toString());String fractionalPart = (java.lang.String) (""«»);String integerPart = (java.lang.String)( myLeadingZero.booleanValue() == true ? "0" : "" ); if (decimalPlaces.intValue()>integerValue.length()) { fractionalPart = repeat(decimalPlaces.intValue()-integerValue.length()) + integerValue; } else { integerPart = integerValue.substring(0,(integerValue.length()-decimalPlaces.intValue())); fractionalPart = integerValue.substring(integerValue.length()-decimalPlaces.intValue()); } if (decimalPlaces.intValue()==0) { decimalSeparator = ""; }return integerPart + decimalSeparator + fractionalPart ;}public static String repeat(int i){ String returnValue = ""; for(int j = 0; j < i; j++) { returnValue = returnValue + "0"; } return returnValue;} You call it with four parameters: the Integer valuethe number of decimal places (as Integer)the decimal separator (as String)whether you want to force a leading zero (as Boolean) So you would set your expression to be something like: $P{REPORT_SCRIPTLET}.formatDecimal($F{your IntegerField},new java.lang.Integer(3),""",new java.lang.Boolean(true)) Of course you can replace the static parameter settings with parameters or variables. If you want to handle errors (like negative number of decimal places) then you'll need to add that into the code. Link to comment Share on other sites More sharing options...
dojo1234 Posted March 16, 2007 Author Share Posted March 16, 2007 Hello it works - thank you for your help. Your Java is better than mine. Regards dojo Link to comment Share on other sites More sharing options...
jmurray Posted March 16, 2007 Share Posted March 16, 2007 I just noticed a mistake in the posting. The double quote should be escaped with a backslash, but when I pasted it into the forum the backslash got stripped. So the example that read $P{REPORT_SCRIPTLET}.formatDecimal($F{your IntegerField},new java.lang.Integer(3),""",new java.lang.Boolean(true)) should actually have read Code:$P{REPORT_SCRIPTLET}.formatDecimal($F{your IntegerField},new java.lang.Integer(3),""",new java.lang.Boolean(true)) .Post edited by: jmurray, at: 2007/03/16 10:39 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now