输入框保留两位小数点,元转分

转载元转分:https://blog.csdn.net/he20101020/article/details/7801190  

转载 保留两位小数:https://blog.csdn.net/s_alics/article/details/77152592

不居中增加 

android:inputType="numberDecimal"


etWithMani是你的输入框ID
etWithMani.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_VARIATION_NORMAL);
etWithMani.addTextChangedListener(new TextWatcher() {

    private int selectionEnd;
    private int selectionStart;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        selectionStart = etWithMani.getSelectionStart();
        selectionEnd = etWithMani.getSelectionEnd();

        if (!isOnlyPointNumber(etWithMani.getText().toString())&& s.length() > 0){
         showToast("最少只有分");
            //删除多余输入的字(不会显示出来)
            s.delete(selectionStart - 1, selectionEnd);
            etWithMani.setText(s);
            etWithMani.setSelection(s.length());
        }
    }
});
 
 

public static boolean isOnlyPointNumber(String number) {
//保留两位小数正则
    Pattern pattern = Pattern.compile("^\\d+\\.?\\d{0,2}$");
    Matcher matcher = pattern.matcher(number);
    return matcher.matches();
}
 
 

这样,两位小数点就完成了,但是考虑到小数很多都是用来进行支付等,输入的也是元为单位,需要转化成分提交到后台,所以,我们还需要一个转换

java金额元与分转换:
1.元转分:
可传入字符串或者long型(隐式转换为long型也可)整数。
若传入为字符串,则通过替换小数点的方式转换,小数点有多位或者没有小数点皆已考虑。
若传入为long 则通过BigDecimal来乘100
2.分转元:

可传入字符串或者long型(隐式转换为long型也可)整数。并对传入的分进行粗略的格式判断

[java]  view plain  copy
  1. package com.westsoft.xpf.portal.utils;  
  2. import java.math.BigDecimal;    
  3.   
  4. /**  
  5.  * com.util.AmountUtils  
  6.  * @description  金额元分之间转换工具类  
  7.  * @author [email protected]  
  8.  * @2012-2-7下午12:58:00  
  9.  */    
  10. public class AmountUtils {    
  11.         
  12.     /**金额为分的格式 */    
  13.     public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";    
  14.         
  15.     /**   
  16.      * 将分为单位的转换为元并返回金额格式的字符串 (除100)  
  17.      *   
  18.      * @param amount  
  19.      * @return  
  20.      * @throws Exception   
  21.      */    
  22.     public static String changeF2Y(Long amount) throws Exception{    
  23.         if(!amount.toString().matches(CURRENCY_FEN_REGEX)) {    
  24.             throw new Exception("金额格式有误");    
  25.         }    
  26.             
  27.         int flag = 0;    
  28.         String amString = amount.toString();    
  29.         if(amString.charAt(0)=='-'){    
  30.             flag = 1;    
  31.             amString = amString.substring(1);    
  32.         }    
  33.         StringBuffer result = new StringBuffer();    
  34.         if(amString.length()==1){    
  35.             result.append("0.0").append(amString);    
  36.         }else if(amString.length() == 2){    
  37.             result.append("0.").append(amString);    
  38.         }else{    
  39.             String intString = amString.substring(0,amString.length()-2);    
  40.             for(int i=1; i<=intString.length();i++){    
  41.                 if( (i-1)%3 == 0 && i !=1){    
  42.                     result.append(",");    
  43.                 }    
  44.                 result.append(intString.substring(intString.length()-i,intString.length()-i+1));    
  45.             }    
  46.             result.reverse().append(".").append(amString.substring(amString.length()-2));    
  47.         }    
  48.         if(flag == 1){    
  49.             return "-"+result.toString();    
  50.         }else{    
  51.             return result.toString();    
  52.         }    
  53.     }    
  54.         
  55.     /**  
  56.      * 将分为单位的转换为元 (除100)  
  57.      *   
  58.      * @param amount  
  59.      * @return  
  60.      * @throws Exception   
  61.      */    
  62.     public static String changeF2Y(String amount) throws Exception{    
  63.         if(!amount.matches(CURRENCY_FEN_REGEX)) {    
  64.             throw new Exception("金额格式有误");    
  65.         }    
  66.         return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();    
  67.     }    
  68.         
  69.     /**   
  70.      * 将元为单位的转换为分 (乘100)  
  71.      *   
  72.      * @param amount  
  73.      * @return  
  74.      */    
  75.     public static String changeY2F(Long amount){    
  76.         return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();    
  77.     }    
  78.         
  79.     /**   
  80.      * 将元为单位的转换为分 替换小数点,支持以逗号区分的金额  
  81.      *   
  82.      * @param amount  
  83.      * @return  
  84.      */    
  85.     public static String changeY2F(String amount){    
  86.         String currency =  amount.replaceAll("\\$|\\¥|\\,""");  //处理包含, ¥ 或者$的金额    
  87.         int index = currency.indexOf(".");    
  88.         int length = currency.length();    
  89.         Long amLong = 0l;    
  90.         if(index == -1){    
  91.             amLong = Long.valueOf(currency+"00");    
  92.         }else if(length - index >= 3){    
  93.             amLong = Long.valueOf((currency.substring(0, index+3)).replace("."""));    
  94.         }else if(length - index == 2){    
  95.             amLong = Long.valueOf((currency.substring(0, index+2)).replace(".""")+0);    
  96.         }else{    
  97.             amLong = Long.valueOf((currency.substring(0, index+1)).replace(".""")+"00");    
  98.         }    
  99.         return amLong.toString();    
  100.     }    
  101.         
  102.     public static void main(String[] args) {    
  103. //        try {    
  104. //            System.out.println("结果:"+changeF2Y("-000a00"));    
  105. //        } catch(Exception e){    
  106. //            System.out.println("----------->>>"+e.getMessage());    
  107. ////          return e.getErrorCode();    
  108. //        }     
  109. //      System.out.println("结果:"+changeY2F("1.00000000001E10"));    
  110.             
  111.         System.out.println(AmountUtils.changeY2F("1.33"));    
  112.         try {  
  113.             System.out.println(AmountUtils.changeF2Y("1322"));  
  114.         } catch (Exception e) {  
  115.             e.printStackTrace();  
  116.         }  
  117. //        System.out.println(Long.parseLong(AmountUtils.changeY2F("1000000000000000")));    
  118. //        System.out.println(Integer.parseInt(AmountUtils.changeY2F("10000000")));    
  119. //        System.out.println(Integer.MIN_VALUE);    
  120. //        long a = 0;    
  121. //        System.out.println(a);    
  122.             
  123.     }    
  124. }    

猜你喜欢

转载自blog.csdn.net/liu_ser/article/details/80137259