Jump to content

Number Formating


dojo1234

Recommended Posts

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

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 value

the 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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...