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

[784 byte] By [leojhartiva] at [2007-11-26 21:17:51]
# 1
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
warnerjaa at 2007-7-10 2:56:23 > top of Java-index,Java Essentials,Java Programming...
# 2
On a DecimalFormat object, call setMultiplier(100).
OleVVa at 2007-7-10 2:56:24 > top of Java-index,Java Essentials,Java Programming...
# 3

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

}

}

OleVVa at 2007-7-10 2:56:24 > top of Java-index,Java Essentials,Java Programming...
# 4
I think your question was answered. Did a herd of buffalos run over you, or are you otherwise detained from saying a simple "thank you"?
warnerjaa at 2007-7-10 2:56:24 > top of Java-index,Java Essentials,Java Programming...
# 5

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)

warnerjaa at 2007-7-10 2:56:24 > top of Java-index,Java Essentials,Java Programming...