NumberFormat.getPercentageInstance() without %
Hello,
In my Spring MVC application I'm trying to use a NumberFormat to bind a specific form field to a CustomNumberEditor (don't worry, this question isn't Spring-specific). In order to do so, I need to create a CustomNumberEditor, suppying a NumberFormat:
new org.springframework.beans.propertyeditors.CustomNumberEditor(
Float.class, NumberFormat.getPercentInstance(),
false));
The problem is that when a getPercentageInstance() NumberFormat is applied to a decimal value, it returns a resulting that is x * 100 and appends a % sign to the end of it. My question is is there any way to have it so the % symbol is not included in the resulting output?
Thanks,
Leo
Construct and use a DecimalFormat object instead, and give it the desired format mask yourself.Edit: Hmmm, except I don't know if it'll handle the automatic multiplication of the input value by 100.Message was edited by: warnerja
The following is a hack. However, if getPercentInstance() gives you a format with some information you'd like to have that you don't get from another DecimalFormat instance, it may be worth it.
NumberFormat nf = NumberFormat.getPercentInstance();
if (nf instanceof DecimalFormat)
{ DecimalFormat df = (DecimalFormat) nf;
String pattern = df.toLocalizedPattern();
// System.out.println(pattern);
int percentSignIndex = pattern.indexOf('%');
if (percentSignIndex > -1)
{ pattern = pattern.substring(0, percentSignIndex)
+ pattern.substring(percentSignIndex + 1);
// System.out.println(pattern);
df.applyLocalizedPattern(pattern);
df.setMultiplier(100);
System.out.println(df.format(0.488)); // prints 49
}
}
Well I guess you must have died or something. So now I guess it won't matter if a bunch of spam gets sent to your email address which is apparently:
leo.hart@fmr.com
leo.hart@fmr.com
leo.hart@fmr.com
leo.hart@fmr.com
leo.hart@fmr.com
(Attention email address harvesters for SPAM bots: See email address above)