on 08-03-2011 7:42 PM
Hello,
When I process the value 782.785 through format number 0.00 I'm expecting a value 782.79 but the standard Format number function returns 782.78. Standard function appears to round to a higher digit only if the value is greater than 5 like '.786' to '.79' and not rounds up the value for 5.
What could be done to achieve this functionality? is it possible without a UDF ?
Thanks for your help!
Larry.
Hi Larry,
You can try the following UDF
public String formatDec(String s,Container container){
try
{
s=new Double(new Double(s).doubleValue()+ new Double("0.001").doubleValue()).toString();
java.text.DecimalFormat df=new java.text.DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
df.setGroupingUsed(false);
s=df.format((new Double(s).doubleValue()));
}
catch(Exception e)
{
return s;
}
return s;
}
here is the input and output I received
input=782.785 output=782.79
input=0.0 output=0.00
input=7.784 output=7.78
input=782.790 output=782.79
regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I have tested format number in both PI7.0 and 7.1. When i pass the input 782.785 and use format number (0.00). In both PI versions i got the output 782.79 only. Please chekc once again.
Thanks,
Soumya.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Round up and Round down requirements is not fully feasible to implement using standard function. Use this logic in your UDF...
If you declare input as string and return the value also string for the decimal number then below logic
String str = "782.666";
double d = Double.valueOf(str);
DecimalFormat de = new DecimalFormat("###.##");
return (Double.valueOf(de.format(d))).toString();
Note: if you declared input as double or float and return the value double then the below logic will work.
double d = 782.666;
DecimalFormat de = new DecimalFormat("###.##");
return Double.valueOf(de.format(d));
Hope that helps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I can't trust math functions like round etc.... especially in pi 7.0 Sp15,
Please use math functions carefully .. example.. in pi 7.0 sp 15 round(123456789.3) is not 123456789.
i suggest to you use java udf. before finalizing test perfectly especially with large numbers and -ve numbers...
Best Regards,
Subbu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
For your requirement you don't use Format Number function.
You have to use Round
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.