package util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.opensymphony.xwork2.util.TypeConversionException;


public class CurrencyBigDecimalConverter extends StrutsTypeConverter {

        @SuppressWarnings("unchecked")
        @Override
        public Object convertFromString(Map contextMap, String[] values, Class 
toClass) {
                BigDecimal result = null;
                if (values != null) {
                        if (values.length != 1) {
                                super.performFallbackConversion(context, 
values, toClass);
                        }
                        
                        if (values.length > 0) {
                                NumberFormat formater = new 
DecimalFormat("#,##0.00");
                                try{
                                        Number tmpResult = 
formater.parse(values[0]);
                                        result = new 
BigDecimal(tmpResult.doubleValue());
                                } catch (ParseException e) {    }
                                
                                if (result == null) { 
                        throw new TypeConversionException("Invalid number 
format: "+values[0]); 
                    } 
                        }
                        
                        
                }
                
                return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        public String convertToString(Map contextMap, Object objectValue) {
                if(objectValue instanceof BigDecimal){
                        NumberFormat formater = new DecimalFormat("#,##0.00");
                        
                        BigDecimal value = (BigDecimal) objectValue;
                        return formater.format(value);
                }
                else{
                        return null;
                }

        }

}

-----Original Message-----
From: Milan Milanovic [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, July 23, 2008 1:02 PM
To: user@struts.apache.org
Subject: Re: [s2] Formatting input textfields


Hi,

could you give here your source code for that converter ?

--
Thx, Milan


aretobios wrote:
> 
> I had the same problem. I solved it by creating a custom type conversion
> for bigdecimal fields.
> 
> Take a look 
> http://struts.apache.org/2.0.11.2/docs/type-conversion.html#TypeConversion-ApplyingaTypeConverterforanapplication
> here 
> 
> By creating your own converter you can have fields were the user write
> number with the #.###,## format and the bigdecimals are displayed with the
> same format
> 
>>
>> Hi,
>>
>> I need to format input in textfields that user can enter value like this:
>> 204,05, but now it is like this 204.05.
>> I have defined this formatting for output already. How can I change this
>> ?
>>
>> --
>> Thx, Milan
>> --
>> View this message in context:
>> http://www.nabble.com/-s2--Formatting-input-textfields-tp18593985p18593985.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>  
> 
> John Koutros
> Technology
> 
> PROTONBANK
> 20 Amaliados & Eslin Str., 115 23  Athens, Greece 
> Tel.: +30 210 6970677, +30 211 1081677
> Mob.: +30 693 6104677 FAX: +30 210 90025299
> eMail: [EMAIL PROTECTED], Web: www.proton.gr
> 
> --------------------------------------------------------
> 
> 
> This message may contain confidential information and is intended only for
> the individual named. If you are not the named addressee you should not
> disseminate, distribute, alter or copy this e-mail. Please notify the
> sender immediately by e-mail if you have received this e-mail by mistake
> and then delete this e-mail from your system. E-mail transmissions cannot
> be guaranteed to be secure, or error-free or virus-free. Any liability for
> all the above is excluded to the fullest extent permitted by law.
> 
> Αυτό το μήνυμα πιθανόν να περιέχει εμπιστευτικές πληροφορίες και
> προορίζεται μόνο για το άτομο που κατονομάζεται. Εάν δεν είστε ο
> κατονομαζόμενος παραλήπτης, δεν πρέπει να διαδώσετε, να διανείμετε, να
> αλλάξετε ή να αντιγράψετε αυτό το ηλεκτρονικό μήνυμα. Παρακαλώ ειδοποιήστε
> τον αποστολέα αμέσως με ηλεκτρονικό ταχυδρομείο εάν έχετε λάβει αυτό το
> ηλεκτρονικό μήνυμα από λάθος και εν συνεχεία διαγράψτε το από το σύστημά
> σας. Η μετάδοση μέσω ηλεκτρονικού ταχυδρομείου δεν παρέχει εγγυήσεις ως
> προς την ασφάλεια, την έλλειψη λαθών ή τη μετάδοση ιών. Οποιαδήποτε ευθύνη
> σχετικά με τα ανωτέρω αποκλείεται κατά το μέγιστο δυνατό μέτρο που
> επιτρέπει ο νόμος.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/-s2--Formatting-input-textfields-tp18593985p18607495.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to