| 1 |
/* |
| 2 |
* @(#)PercentConverter.java 4/10/2007 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2007 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.converter; |
| 8 |
|
| 9 |
import java.text.NumberFormat; |
| 10 |
import java.util.Locale; |
| 11 |
|
| 12 |
/** |
| 13 |
* Converter which converts percentage to String and converts it back. |
| 14 |
*/ |
| 15 |
public class PercentConverter extends NumberFormatConverter { |
| 16 |
public static ConverterContext CONTEXT = new ConverterContext("Percent"); |
| 17 |
|
| 18 |
public PercentConverter() { |
| 19 |
this(NumberFormat.getPercentInstance(Locale.getDefault())); |
| 20 |
} |
| 21 |
|
| 22 |
public PercentConverter(NumberFormat format) { |
| 23 |
super(format); |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public Object fromString(String string, ConverterContext context) { |
| 28 |
Object o = super.fromString(string, context); |
| 29 |
if (string != null && !string.trim().endsWith("%") && o instanceof Number && ((Number) o).doubleValue() > 1) { |
| 30 |
o = ((Number) o).doubleValue() / 100; |
| 31 |
} |
| 32 |
return o; |
| 33 |
} |
| 34 |
} |