Saturday, 27 April 2013

Number Formater using custom decimal points in Android


Here is the post for set number format in values with any of decimal points and Group separator as well. 

public String setNumberFormat(Float numval)
       {            
              String numformateString=123,456,789.00;

              DecimalFormat fmt = new DecimalFormat();
              DecimalFormatSymbols fmts = new DecimalFormatSymbols();
              fmt.setGroupingUsed(true);

              if(numformateString.equalsIgnoreCase("123,456,789.00"))
              {
                     fmts.setGroupingSeparator(',');         
                     fmts.setDecimalSeparator('.');   

              }
              else if(numformateString.equalsIgnoreCase("123.456.789,00"))
              {
                     fmts.setGroupingSeparator('.');         
                     fmts.setDecimalSeparator(',');
                     fmts.setPerMill('.');                    

              }
              else if(numformateString.equalsIgnoreCase("123 456 789,00"))
              {
                     fmts.setGroupingSeparator(' ');         
                     fmts.setDecimalSeparator(',');
                     fmts.setPerMill(' ');
              }
              else
              {

                     fmts.setGroupingSeparator('\'');        
                     fmts.setDecimalSeparator(',');

              }
              fmt.setDecimalFormatSymbols(fmts);                    
              return fmt.format(numval);
       }